Skip to content

Commit 0b21a5b

Browse files
committed
display error message to customer
1 parent d6dbd1f commit 0b21a5b

File tree

3 files changed

+196
-27
lines changed

3 files changed

+196
-27
lines changed

src/Network/Network/NetworkManager/VnetVerifier/ReachabilityAnalysisIntentBaseCmdlet.cs

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using Microsoft.Azure.Management.Network;
1717
using System.Net;
1818
using Microsoft.Azure.Commands.Network.Models.NetworkManager;
19+
using Newtonsoft.Json.Linq;
20+
using System;
1921

2022
namespace Microsoft.Azure.Commands.Network
2123
{
@@ -37,16 +39,71 @@ public bool IsAnalysisIntentPresent(string resourceGroupName, string networkMana
3739
}
3840
catch (Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
3941
{
40-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
42+
// Use the concise error handling method
43+
HandleError(exception);
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
50+
// Helper method for concise error handling
51+
private void HandleError(Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
52+
{
53+
switch (exception.Response.StatusCode)
54+
{
55+
case HttpStatusCode.BadRequest:
56+
// Specific handling for Bad Request with detailed message
57+
DisplayDetailedErrorMessage(exception);
58+
break;
59+
60+
case HttpStatusCode.NotFound:
61+
WriteWarning("Error: Not Found - The specified resource could not be found.");
62+
break;
63+
64+
case HttpStatusCode.Forbidden:
65+
WriteWarning("Error: Forbidden - You do not have permission to perform this operation.");
66+
break;
67+
68+
case HttpStatusCode.InternalServerError:
69+
WriteWarning("Error: Internal Server Error - The server encountered an unexpected condition. Try again later.");
70+
break;
71+
72+
default:
73+
WriteWarning($"Error: {exception.Response.StatusCode} - {exception.Message}");
74+
break;
75+
}
76+
}
77+
78+
// Method to display a detailed error message for BadRequest (400) responses
79+
private void DisplayDetailedErrorMessage(Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
80+
{
81+
string errorMessage = "Bad Request: An unknown error occurred.";
82+
83+
// Check if the response content is available
84+
if (!string.IsNullOrEmpty(exception.Response.Content))
85+
{
86+
try
4187
{
42-
// Resource is not present
43-
return false;
88+
// Parse the JSON response content to get the "message" field
89+
var errorContent = JObject.Parse(exception.Response.Content);
90+
errorMessage = errorContent["message"]?.ToString() ?? errorMessage;
91+
}
92+
catch
93+
{
94+
// Fallback if parsing fails
95+
WriteWarning($"Bad Request: Unable to parse error details. Raw response: {exception.Response.Content}");
96+
return;
4497
}
45-
46-
throw;
98+
}
99+
else
100+
{
101+
// If there is no content, default message
102+
errorMessage = "Bad Request: The request was invalid. Please check your parameters.";
47103
}
48104

49-
return true;
105+
// Display the error message to the user
106+
WriteWarning(errorMessage);
50107
}
51108

52109
public PSReachabilityAnalysisIntent GetAnalysisIntent(string resourceGroupName, string networkManagerName, string workspaceName, string analysisIntentName)

src/Network/Network/NetworkManager/VnetVerifier/ReachabilityAnalysisRunBaseCmdlet.cs

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System.Net;
1818
using Microsoft.Azure.Commands.Network.Models.NetworkManager;
1919
using System.Linq;
20+
using Newtonsoft.Json;
21+
using Newtonsoft.Json.Linq;
2022

2123
namespace Microsoft.Azure.Commands.Network
2224
{
@@ -38,27 +40,81 @@ public bool IsAnalysisRunPresent(string resourceGroupName, string networkManager
3840
}
3941
catch (Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
4042
{
41-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
42-
{
43-
// Resource is not present
44-
return false;
45-
}
46-
47-
throw;
43+
// Use the concise error handling method
44+
HandleError(exception);
45+
return false;
4846
}
49-
5047
return true;
5148
}
5249

5350
public PSReachabilityAnalysisRun GetAnalysisRun(string resourceGroupName, string networkManagerName, string workspaceName, string analysisRunName)
5451
{
55-
var analysisRun = this.ReachabilityAnalysisRunClient.Get(resourceGroupName, networkManagerName, workspaceName, analysisRunName);
56-
var psAnalysisRun = ToPsReachabilityAnalysisRun(analysisRun);
57-
psAnalysisRun.ResourceGroupName = resourceGroupName;
58-
psAnalysisRun.NetworkManagerName = networkManagerName;
59-
psAnalysisRun.VerifierWorkspaceName = workspaceName;
60-
psAnalysisRun.Name = analysisRunName;
61-
return psAnalysisRun;
52+
var analysisRun = this.ReachabilityAnalysisRunClient.Get(resourceGroupName, networkManagerName, workspaceName, analysisRunName);
53+
var psAnalysisRun = ToPsReachabilityAnalysisRun(analysisRun);
54+
psAnalysisRun.ResourceGroupName = resourceGroupName;
55+
psAnalysisRun.NetworkManagerName = networkManagerName;
56+
psAnalysisRun.VerifierWorkspaceName = workspaceName;
57+
psAnalysisRun.Name = analysisRunName;
58+
return psAnalysisRun;
59+
}
60+
61+
// Helper method for concise error handling
62+
private void HandleError(Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
63+
{
64+
switch (exception.Response.StatusCode)
65+
{
66+
case HttpStatusCode.BadRequest:
67+
// Specific handling for Bad Request with detailed message
68+
DisplayDetailedErrorMessage(exception);
69+
break;
70+
71+
case HttpStatusCode.NotFound:
72+
WriteWarning("Error: Not Found - The specified resource could not be found.");
73+
break;
74+
75+
case HttpStatusCode.Forbidden:
76+
WriteWarning("Error: Forbidden - You do not have permission to perform this operation.");
77+
break;
78+
79+
case HttpStatusCode.InternalServerError:
80+
WriteWarning("Error: Internal Server Error - The server encountered an unexpected condition. Try again later.");
81+
break;
82+
83+
default:
84+
WriteWarning($"Error: {exception.Response.StatusCode} - {exception.Message}");
85+
break;
86+
}
87+
}
88+
89+
// Method to display a detailed error message for BadRequest (400) responses
90+
private void DisplayDetailedErrorMessage(Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
91+
{
92+
string errorMessage = "Bad Request: An unknown error occurred.";
93+
94+
// Check if the response content is available
95+
if (!string.IsNullOrEmpty(exception.Response.Content))
96+
{
97+
try
98+
{
99+
// Parse the JSON response content to get the "message" field
100+
var errorContent = JObject.Parse(exception.Response.Content);
101+
errorMessage = errorContent["message"]?.ToString() ?? errorMessage;
102+
}
103+
catch
104+
{
105+
// Fallback if parsing fails
106+
WriteWarning($"Bad Request: Unable to parse error details. Raw response: {exception.Response.Content}");
107+
return;
108+
}
109+
}
110+
else
111+
{
112+
// If there is no content, default message
113+
errorMessage = "Bad Request: The request was invalid. Please check your parameters.";
114+
}
115+
116+
// Display the error message to the user
117+
WriteWarning(errorMessage);
62118
}
63119

64120
public PSReachabilityAnalysisRun ToPsReachabilityAnalysisRun(Management.Network.Models.ReachabilityAnalysisRun analysisRun)

src/Network/Network/NetworkManager/VnetVerifier/VerifierWorkspaceBaseCmdlet.cs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Management.Network;
1717
using System.Net;
1818
using Microsoft.Azure.Commands.Network.Models.NetworkManager;
19+
using Newtonsoft.Json.Linq;
1920

2021
namespace Microsoft.Azure.Commands.Network
2122
{
@@ -37,16 +38,71 @@ public bool IsVerifierWorkspacePresent(string resourceGroupName, string networkM
3738
}
3839
catch (Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
3940
{
40-
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
41+
// Use the concise error handling method
42+
HandleError(exception);
43+
return false;
44+
}
45+
46+
return true;
47+
}
48+
49+
// Helper method for concise error handling
50+
private void HandleError(Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
51+
{
52+
switch (exception.Response.StatusCode)
53+
{
54+
case HttpStatusCode.BadRequest:
55+
// Specific handling for Bad Request with detailed message
56+
DisplayDetailedErrorMessage(exception);
57+
break;
58+
59+
case HttpStatusCode.NotFound:
60+
WriteWarning("Error: Not Found - The specified resource could not be found.");
61+
break;
62+
63+
case HttpStatusCode.Forbidden:
64+
WriteWarning("Error: Forbidden - You do not have permission to perform this operation.");
65+
break;
66+
67+
case HttpStatusCode.InternalServerError:
68+
WriteWarning("Error: Internal Server Error - The server encountered an unexpected condition. Try again later.");
69+
break;
70+
71+
default:
72+
WriteWarning($"Error: {exception.Response.StatusCode} - {exception.Message}");
73+
break;
74+
}
75+
}
76+
77+
// Method to display a detailed error message for BadRequest (400) responses
78+
private void DisplayDetailedErrorMessage(Microsoft.Azure.Management.Network.Models.CommonErrorResponseException exception)
79+
{
80+
string errorMessage = "Bad Request: An unknown error occurred.";
81+
82+
// Check if the response content is available
83+
if (!string.IsNullOrEmpty(exception.Response.Content))
84+
{
85+
try
4186
{
42-
// Resource is not present
43-
return false;
87+
// Parse the JSON response content to get the "message" field
88+
var errorContent = JObject.Parse(exception.Response.Content);
89+
errorMessage = errorContent["message"]?.ToString() ?? errorMessage;
90+
}
91+
catch
92+
{
93+
// Fallback if parsing fails
94+
WriteWarning($"Bad Request: Unable to parse error details. Raw response: {exception.Response.Content}");
95+
return;
4496
}
45-
46-
throw;
97+
}
98+
else
99+
{
100+
// If there is no content, default message
101+
errorMessage = "Bad Request: The request was invalid. Please check your parameters.";
47102
}
48103

49-
return true;
104+
// Display the error message to the user
105+
WriteWarning(errorMessage);
50106
}
51107

52108

0 commit comments

Comments
 (0)