17
17
using System . Net ;
18
18
using Microsoft . Azure . Commands . Network . Models . NetworkManager ;
19
19
using System . Linq ;
20
+ using Newtonsoft . Json ;
21
+ using Newtonsoft . Json . Linq ;
20
22
21
23
namespace Microsoft . Azure . Commands . Network
22
24
{
@@ -38,27 +40,81 @@ public bool IsAnalysisRunPresent(string resourceGroupName, string networkManager
38
40
}
39
41
catch ( Microsoft . Azure . Management . Network . Models . CommonErrorResponseException exception )
40
42
{
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 ;
48
46
}
49
-
50
47
return true ;
51
48
}
52
49
53
50
public PSReachabilityAnalysisRun GetAnalysisRun ( string resourceGroupName , string networkManagerName , string workspaceName , string analysisRunName )
54
51
{
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 ) ;
62
118
}
63
119
64
120
public PSReachabilityAnalysisRun ToPsReachabilityAnalysisRun ( Management . Network . Models . ReachabilityAnalysisRun analysisRun )
0 commit comments