Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CloudError code into exception for tracking #14077

Merged
merged 4 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Tracked CloudError code in exception
* Invoked on clear context listener in Azure session to be cleared when `Clear-AzContext` was executed

## Version 2.2.4
Expand Down
50 changes: 43 additions & 7 deletions src/Accounts/Accounts/CommonModule/AzModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.Commands.Profile.CommonModule;
using Microsoft.Rest;
using Microsoft.Rest.Azure;
using Microsoft.Rest.Serialization;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -42,14 +45,35 @@ public class AzModule : IDisposable
ICommandRuntime _runtime;
TelemetryProvider _telemetry;
AdalLogger _logger;
internal static readonly string[] ClientHeaders = {"x-ms-client-request-id", "client-request-id", "x-ms-request-id", "request-id" };
internal static readonly string[] ClientHeaders = { "x-ms-client-request-id", "client-request-id", "x-ms-request-id", "request-id" };

private static JsonSerializerSettings DeserializationSettings = null;
static AzModule()
{
DeserializationSettings = new JsonSerializerSettings
{
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter>
{
new Iso8601TimeSpanConverter()
}
};
DeserializationSettings.Converters.Add(new TransformationJsonConverter());
DeserializationSettings.Converters.Add(new CloudErrorJsonConverter());
}


public AzModule(ICommandRuntime runtime, IEventStore eventHandler)
{
_runtime = runtime;
_deferredEvents = eventHandler;
_logger = new AdalLogger(_deferredEvents.GetDebugLogger());
_telemetry = TelemetryProvider.Create(
_deferredEvents.GetWarningLogger(), _deferredEvents.GetDebugLogger());
_deferredEvents.GetWarningLogger(), _deferredEvents.GetDebugLogger());
}

public AzModule(ICommandRuntime runtime) : this(runtime, new EventStore())
Expand Down Expand Up @@ -174,9 +198,9 @@ await signal(Events.Debug, cancellationToken,
if (data?.ResponseMessage is HttpResponseMessage response)
{
try {
// Print formatted response message
await signal(Events.Debug, cancellationToken,
() => EventHelper.CreateLogEvent(GeneralUtilities.GetLog(response)));
// Print formatted response message
await signal(Events.Debug, cancellationToken,
() => EventHelper.CreateLogEvent(GeneralUtilities.GetLog(response)));
} catch {
// response was disposed, ignore
}
Expand Down Expand Up @@ -221,9 +245,21 @@ internal async Task OnFinally(string id, CancellationToken cancellationToken, Ge
{
if(!response.IsSuccessStatusCode && qos.Exception == null)
{
// add "InternalException" as message because it is just for telemtry tracking.
AzPSCloudException ex = (response.StatusCode == HttpStatusCode.NotFound) ?
new AzPSResourceNotFoundCloudException(String.Empty) : new AzPSCloudException(String.Empty);
ex.Response = new HttpResponseMessageWrapper(response, String.Empty);
new AzPSResourceNotFoundCloudException("InternalException") : new AzPSCloudException("InternalException");
try
{
string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
CloudError cloudError = SafeJsonConvert.DeserializeObject<CloudError>(responseContent, DeserializationSettings);
ex.Body = cloudError;
ex.Data[AzurePSErrorDataKeys.CloudErrorCodeKey] = cloudError.Code;
}
catch (Exception exception)
{
await signal(Events.Debug, cancellationToken,
() => EventHelper.CreateLogEvent($"[{id}]: Cannot deserialize due to {exception.Message}"));
}
qos.Exception = ex;
await signal(Events.Debug, cancellationToken,
() => EventHelper.CreateLogEvent($"[{id}]: Getting exception '{qos.Exception}' from response"));
Expand Down