From 6e7863522bf0dac3108f9d063c6cc987440a0158 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 1 Feb 2022 12:02:04 +0100 Subject: [PATCH 1/2] ... --- .../Client/Pages/Gateways/Detail.razor | 20 +++++++++++++------ .../Managers/ConnectionStringManager.cs | 5 +++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor b/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor index fed2a74e3..ef904d829 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor @@ -269,16 +269,24 @@ else public async Task ShowConnectionString() { - gateway.SymmetricKey = await this.Http.GetStringAsync($"api/Gateways/{gateway.DeviceId}/ConnectionString"); + try + { + gateway.SymmetricKey = await this.Http.GetStringAsync($"api/Gateways/{gateway.DeviceId}/ConnectionString"); - var parameter = new DialogParameters(); + var parameter = new DialogParameters(); - parameter.Add(nameof(Gateway.DeviceId), gateway.DeviceId); - parameter.Add(nameof(Gateway.EndPoint), gateway.EndPoint); - parameter.Add(nameof(Gateway.SymmetricKey), gateway.SymmetricKey); + parameter.Add(nameof(Gateway.DeviceId), gateway.DeviceId); + parameter.Add(nameof(Gateway.EndPoint), gateway.EndPoint); + parameter.Add(nameof(Gateway.SymmetricKey), gateway.SymmetricKey); - DialogService.Show("ConnectionString gateway ", parameter); + DialogService.Show("ConnectionString gateway ", parameter); + } + catch ( HttpRequestException e) + { + Console.WriteLine(e); + Snackbar.Add($"{e}", Severity.Error); + } } public async Task ShowDeleteModal() diff --git a/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs b/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs index b1aab1013..bf6de0250 100644 --- a/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs +++ b/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs @@ -6,6 +6,7 @@ namespace AzureIoTHub.Portal.Server.Managers using System.Threading.Tasks; using AzureIoTHub.Portal.Server.Helpers; using AzureIoTHub.Portal.Server.Services; + using Microsoft.Azure.Devices.Provisioning.Service; public class ConnectionStringManager : IConnectionStringManager { @@ -24,6 +25,10 @@ public async Task GetSymmetricKey(string deviceId) return DeviceHelper.RetrieveSymmetricKey(deviceId, attestationMechanism); } + catch (ProvisioningServiceClientException) + { + throw new System.Exception("The enrollment group does not exist."); + } catch (System.Exception e) { throw new System.Exception(e.Message); From 1a8e1a3adf9f56ce619f10bd864e356b6db45c74 Mon Sep 17 00:00:00 2001 From: ben salim Date: Tue, 1 Feb 2022 14:11:27 +0100 Subject: [PATCH 2/2] fix #142 --- .../Client/Pages/Gateways/Detail.razor | 10 ++++++++-- .../Server/Controllers/GatewaysController.cs | 5 +++++ .../Server/Managers/ConnectionStringManager.cs | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor b/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor index ef904d829..edb72ec98 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/Gateways/Detail.razor @@ -284,8 +284,14 @@ else } catch ( HttpRequestException e) { - Console.WriteLine(e); - Snackbar.Add($"{e}", Severity.Error); + if (e.StatusCode == System.Net.HttpStatusCode.NotFound) + { + Snackbar.Add("Cannot obtain the connection string
because the enrollment group does not exist.", Severity.Error); + } + else + { + Snackbar.Add($"Something went wrong.", Severity.Error); + } } } diff --git a/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs b/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs index a8ca77352..9d21e5791 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/GatewaysController.cs @@ -15,6 +15,7 @@ namespace AzureIoTHub.Portal.Server.Controllers using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Devices; using Microsoft.Azure.Devices.Common.Exceptions; + using Microsoft.Azure.Devices.Provisioning.Service; using Microsoft.Azure.Devices.Shared; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; @@ -143,6 +144,10 @@ public async Task GetSymmetricKey(string deviceId) { return this.Ok(await this.connectionStringManager.GetSymmetricKey(deviceId)); } + catch (ProvisioningServiceClientException e) + { + return this.NotFound(e.Message); + } catch (Exception e) { return this.BadRequest(e.Message); diff --git a/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs b/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs index bf6de0250..f52b938d1 100644 --- a/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs +++ b/src/AzureIoTHub.Portal/Server/Managers/ConnectionStringManager.cs @@ -27,7 +27,7 @@ public async Task GetSymmetricKey(string deviceId) } catch (ProvisioningServiceClientException) { - throw new System.Exception("The enrollment group does not exist."); + throw new ProvisioningServiceClientException("The enrollment group does not exist."); } catch (System.Exception e) {