13
13
// ----------------------------------------------------------------------------------
14
14
15
15
using System ;
16
+ using System . Collections . Generic ;
17
+ using System . Linq ;
16
18
using System . Management . Automation ;
17
19
using System . Runtime . ExceptionServices ;
20
+ using System . Runtime . InteropServices ;
21
+ using System . Security ;
22
+ using System . Text ;
18
23
using System . Threading ;
19
- using Microsoft . Azure . Commands . MachineLearning . Extensions ;
24
+
20
25
using Microsoft . Azure . Commands . MachineLearning . Utilities ;
21
- using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Entities . ErrorResponses ;
22
- using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Extensions ;
23
26
using Microsoft . Azure . Commands . ResourceManager . Common ;
24
27
using Microsoft . Rest . Azure ;
25
28
29
+ using Microsoft . WindowsAzure . Commands . Common ;
30
+
26
31
namespace Microsoft . Azure . Commands . MachineLearning
27
32
{
28
33
public abstract class WebServicesCmdletBase : AzureRMCmdlet
@@ -77,7 +82,7 @@ protected override void BeginProcessing()
77
82
}
78
83
catch ( Exception ex )
79
84
{
80
- if ( ex . IsFatal ( ) )
85
+ if ( this . IsFatalException ( ex ) )
81
86
{
82
87
ThrowTerminatingError ( new ErrorRecord ( ex , string . Empty , ErrorCategory . InvalidOperation , this ) ) ;
83
88
}
@@ -95,7 +100,7 @@ protected override void EndProcessing()
95
100
}
96
101
catch ( Exception ex )
97
102
{
98
- if ( ex . IsFatal ( ) )
103
+ if ( this . IsFatalException ( ex ) )
99
104
{
100
105
ThrowTerminatingError ( new ErrorRecord ( ex , string . Empty , ErrorCategory . InvalidOperation , this ) ) ;
101
106
}
@@ -123,7 +128,7 @@ protected override void StopProcessing()
123
128
}
124
129
catch ( Exception ex )
125
130
{
126
- if ( ex . IsFatal ( ) )
131
+ if ( this . IsFatalException ( ex ) )
127
132
{
128
133
throw ;
129
134
}
@@ -154,7 +159,7 @@ public override void ExecuteCmdlet()
154
159
}
155
160
catch ( Exception ex )
156
161
{
157
- if ( ex . IsFatal ( ) )
162
+ if ( this . IsFatalException ( ex ) )
158
163
{
159
164
throw ;
160
165
}
@@ -192,11 +197,11 @@ private void HandleException(ExceptionDispatchInfo capturedException)
192
197
var cloudException = capturedException . SourceException as CloudException ;
193
198
if ( cloudException != null )
194
199
{
195
- errorRecord = cloudException . ToErrorRecord ( ) ;
200
+ errorRecord = this . CreateErrorRecordForCloudException ( cloudException ) ; ////new ErrorRecord(cloudException, cloudException.Message, ErrorCategory.CloseError, null );
196
201
}
197
202
else
198
203
{
199
- var errorResponseException =
204
+ var errorResponseException =
200
205
capturedException . SourceException as ErrorResponseMessageException ;
201
206
if ( errorResponseException != null )
202
207
{
@@ -208,20 +213,20 @@ private void HandleException(ExceptionDispatchInfo capturedException)
208
213
capturedException . SourceException as AggregateException ;
209
214
if ( aggregateException != null )
210
215
{
211
- errorResponseException =
216
+ errorResponseException =
212
217
aggregateException . InnerException as ErrorResponseMessageException ;
213
218
if ( errorResponseException != null )
214
219
{
215
220
errorRecord = errorResponseException . ToErrorRecord ( ) ;
216
221
}
217
222
else
218
223
{
219
- errorRecord = aggregateException . InnerException . ToErrorRecord ( ) ;
224
+ errorRecord = new ErrorRecord ( aggregateException . InnerException , aggregateException . InnerException . Message , ErrorCategory . CloseError , null ) ;
220
225
}
221
226
}
222
227
else
223
228
{
224
- errorRecord = capturedException . SourceException . ToErrorRecord ( ) ;
229
+ errorRecord = new ErrorRecord ( capturedException . SourceException , capturedException . SourceException . Message , ErrorCategory . CloseError , null ) ;
225
230
}
226
231
}
227
232
}
@@ -233,5 +238,75 @@ private void HandleException(ExceptionDispatchInfo capturedException)
233
238
this . DisposeOfCancellationSource ( ) ;
234
239
}
235
240
}
241
+
242
+
243
+ /// <summary>
244
+ /// Converts <see cref="CloudException"/> objects into <see cref="ErrorRecord"/>
245
+ /// </summary>
246
+ /// <param name="cloudException">The exception</param>
247
+ private ErrorRecord CreateErrorRecordForCloudException ( CloudException cloudException )
248
+ {
249
+ var errorReport = new StringBuilder ( ) ;
250
+
251
+ string requestId = cloudException . RequestId ;
252
+ if ( string . IsNullOrWhiteSpace ( requestId ) && cloudException . Response != null )
253
+ {
254
+ // Try to obtain the request id from the HTTP response associated with the exception
255
+ IEnumerable < string > headerValues = Enumerable . Empty < string > ( ) ;
256
+ if ( cloudException . Response . Headers != null &&
257
+ cloudException . Response . Headers . TryGetValue ( "x-ms-request-id" , out headerValues ) )
258
+ {
259
+ requestId = headerValues . First ( ) ;
260
+ }
261
+ }
262
+
263
+ errorReport . AppendLine ( ) ;
264
+ errorReport . AppendLine ( "Request Id: {0}" . FormatInvariant ( requestId ) ) ;
265
+ errorReport . AppendLine ( "Error Code: {0}" . FormatInvariant ( cloudException . Body . Code ) ) ;
266
+ errorReport . AppendLine ( "Error Message: {0}" . FormatInvariant ( cloudException . Body . Message ) ) ;
267
+ errorReport . AppendLine ( "Error Target: {0}" . FormatInvariant ( cloudException . Body . Target ) ) ;
268
+ if ( cloudException . Body . Details . Any ( ) )
269
+ {
270
+ errorReport . AppendLine ( "Error Details:" ) ;
271
+ foreach ( var errorDetail in cloudException . Body . Details )
272
+ {
273
+ errorReport . AppendLine ( "\t [Code={0}, Message={1}]" . FormatInvariant ( errorDetail . Code , errorDetail . Message ) ) ;
274
+ }
275
+ }
276
+
277
+ var returnedError = new Exception ( errorReport . ToString ( ) , cloudException ) ;
278
+ return new ErrorRecord ( returnedError , "Resource Provider Error" , ErrorCategory . CloseError , null ) ;
279
+ }
280
+
281
+ /// <summary>
282
+ /// Test if an exception is a fatal exception.
283
+ /// </summary>
284
+ /// <param name="ex">Exception object.</param>
285
+ private bool IsFatalException ( Exception ex )
286
+ {
287
+ if ( ex is AggregateException )
288
+ {
289
+ return ( ( AggregateException ) ex ) . Flatten ( ) . InnerExceptions . Any ( exception => this . IsFatalException ( exception ) ) ;
290
+ }
291
+
292
+ if ( ex . InnerException != null && this . IsFatalException ( ex . InnerException ) )
293
+ {
294
+ return true ;
295
+ }
296
+
297
+ return
298
+ ex is TypeInitializationException ||
299
+ ex is AppDomainUnloadedException ||
300
+ ex is ThreadInterruptedException ||
301
+ ex is AccessViolationException ||
302
+ ex is InvalidProgramException ||
303
+ ex is BadImageFormatException ||
304
+ ex is StackOverflowException ||
305
+ ex is ThreadAbortException ||
306
+ ex is OutOfMemoryException ||
307
+ ex is SecurityException ||
308
+ ex is SEHException ;
309
+ }
310
+
236
311
}
237
312
}
0 commit comments