16
16
using Microsoft . Azure . Commands . Common . Exceptions ;
17
17
using Microsoft . Azure . Commands . Profile . CommonModule ;
18
18
using Microsoft . Rest ;
19
+ using Microsoft . Rest . Azure ;
20
+ using Microsoft . Rest . Serialization ;
19
21
using Microsoft . WindowsAzure . Commands . Utilities . Common ;
22
+ using Newtonsoft . Json ;
20
23
using System ;
21
24
using System . Collections . Generic ;
22
25
using System . Linq ;
@@ -42,14 +45,35 @@ public class AzModule : IDisposable
42
45
ICommandRuntime _runtime ;
43
46
TelemetryProvider _telemetry ;
44
47
AdalLogger _logger ;
45
- internal static readonly string [ ] ClientHeaders = { "x-ms-client-request-id" , "client-request-id" , "x-ms-request-id" , "request-id" } ;
48
+ internal static readonly string [ ] ClientHeaders = { "x-ms-client-request-id" , "client-request-id" , "x-ms-request-id" , "request-id" } ;
49
+
50
+ private static JsonSerializerSettings DeserializationSettings = null ;
51
+ static AzModule ( )
52
+ {
53
+ DeserializationSettings = new JsonSerializerSettings
54
+ {
55
+ DateFormatHandling = Newtonsoft . Json . DateFormatHandling . IsoDateFormat ,
56
+ DateTimeZoneHandling = Newtonsoft . Json . DateTimeZoneHandling . Utc ,
57
+ NullValueHandling = Newtonsoft . Json . NullValueHandling . Ignore ,
58
+ ReferenceLoopHandling = Newtonsoft . Json . ReferenceLoopHandling . Serialize ,
59
+ ContractResolver = new ReadOnlyJsonContractResolver ( ) ,
60
+ Converters = new List < JsonConverter >
61
+ {
62
+ new Iso8601TimeSpanConverter ( )
63
+ }
64
+ } ;
65
+ DeserializationSettings . Converters . Add ( new TransformationJsonConverter ( ) ) ;
66
+ DeserializationSettings . Converters . Add ( new CloudErrorJsonConverter ( ) ) ;
67
+ }
68
+
69
+
46
70
public AzModule ( ICommandRuntime runtime , IEventStore eventHandler )
47
71
{
48
72
_runtime = runtime ;
49
73
_deferredEvents = eventHandler ;
50
74
_logger = new AdalLogger ( _deferredEvents . GetDebugLogger ( ) ) ;
51
75
_telemetry = TelemetryProvider . Create (
52
- _deferredEvents . GetWarningLogger ( ) , _deferredEvents . GetDebugLogger ( ) ) ;
76
+ _deferredEvents . GetWarningLogger ( ) , _deferredEvents . GetDebugLogger ( ) ) ;
53
77
}
54
78
55
79
public AzModule ( ICommandRuntime runtime ) : this ( runtime , new EventStore ( ) )
@@ -174,9 +198,9 @@ await signal(Events.Debug, cancellationToken,
174
198
if ( data ? . ResponseMessage is HttpResponseMessage response )
175
199
{
176
200
try {
177
- // Print formatted response message
178
- await signal ( Events . Debug , cancellationToken ,
179
- ( ) => EventHelper . CreateLogEvent ( GeneralUtilities . GetLog ( response ) ) ) ;
201
+ // Print formatted response message
202
+ await signal ( Events . Debug , cancellationToken ,
203
+ ( ) => EventHelper . CreateLogEvent ( GeneralUtilities . GetLog ( response ) ) ) ;
180
204
} catch {
181
205
// response was disposed, ignore
182
206
}
@@ -221,9 +245,21 @@ internal async Task OnFinally(string id, CancellationToken cancellationToken, Ge
221
245
{
222
246
if ( ! response . IsSuccessStatusCode && qos . Exception == null )
223
247
{
248
+ // add "InternalException" as message because it is just for telemtry tracking.
224
249
AzPSCloudException ex = ( response . StatusCode == HttpStatusCode . NotFound ) ?
225
- new AzPSResourceNotFoundCloudException ( String . Empty ) : new AzPSCloudException ( String . Empty ) ;
226
- ex . Response = new HttpResponseMessageWrapper ( response , String . Empty ) ;
250
+ new AzPSResourceNotFoundCloudException ( "InternalException" ) : new AzPSCloudException ( "InternalException" ) ;
251
+ try
252
+ {
253
+ string responseContent = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
254
+ CloudError cloudError = SafeJsonConvert . DeserializeObject < CloudError > ( responseContent , DeserializationSettings ) ;
255
+ ex . Body = cloudError ;
256
+ ex . Data [ AzurePSErrorDataKeys . CloudErrorCodeKey ] = cloudError . Code ;
257
+ }
258
+ catch ( Exception exception )
259
+ {
260
+ await signal ( Events . Debug , cancellationToken ,
261
+ ( ) => EventHelper . CreateLogEvent ( $ "[{ id } ]: Cannot deserialize due to { exception . Message } ") ) ;
262
+ }
227
263
qos . Exception = ex ;
228
264
await signal ( Events . Debug , cancellationToken ,
229
265
( ) => EventHelper . CreateLogEvent ( $ "[{ id } ]: Getting exception '{ qos . Exception } ' from response") ) ;
0 commit comments