Skip to content

Commit 7cbd0d2

Browse files
authored
Add CloudError code into exception for tracking (#14077)
* Update error kind when context is incorrect * Add cloud error code into exception data * Add changelog
1 parent c216fcd commit 7cbd0d2

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Tracked CloudError code in exception
2122
* Invoked on clear context listener in Azure session to be cleared when `Clear-AzContext` was executed
2223

2324
## Version 2.2.4

src/Accounts/Accounts/CommonModule/AzModule.cs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
using Microsoft.Azure.Commands.Common.Exceptions;
1717
using Microsoft.Azure.Commands.Profile.CommonModule;
1818
using Microsoft.Rest;
19+
using Microsoft.Rest.Azure;
20+
using Microsoft.Rest.Serialization;
1921
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
using Newtonsoft.Json;
2023
using System;
2124
using System.Collections.Generic;
2225
using System.Linq;
@@ -42,14 +45,35 @@ public class AzModule : IDisposable
4245
ICommandRuntime _runtime;
4346
TelemetryProvider _telemetry;
4447
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+
4670
public AzModule(ICommandRuntime runtime, IEventStore eventHandler)
4771
{
4872
_runtime = runtime;
4973
_deferredEvents = eventHandler;
5074
_logger = new AdalLogger(_deferredEvents.GetDebugLogger());
5175
_telemetry = TelemetryProvider.Create(
52-
_deferredEvents.GetWarningLogger(), _deferredEvents.GetDebugLogger());
76+
_deferredEvents.GetWarningLogger(), _deferredEvents.GetDebugLogger());
5377
}
5478

5579
public AzModule(ICommandRuntime runtime) : this(runtime, new EventStore())
@@ -174,9 +198,9 @@ await signal(Events.Debug, cancellationToken,
174198
if (data?.ResponseMessage is HttpResponseMessage response)
175199
{
176200
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)));
180204
} catch {
181205
// response was disposed, ignore
182206
}
@@ -221,9 +245,21 @@ internal async Task OnFinally(string id, CancellationToken cancellationToken, Ge
221245
{
222246
if(!response.IsSuccessStatusCode && qos.Exception == null)
223247
{
248+
// add "InternalException" as message because it is just for telemtry tracking.
224249
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+
}
227263
qos.Exception = ex;
228264
await signal(Events.Debug, cancellationToken,
229265
() => EventHelper.CreateLogEvent($"[{id}]: Getting exception '{qos.Exception}' from response"));

0 commit comments

Comments
 (0)