Skip to content

Commit

Permalink
Merge pull request #11253 from anthony-c-martin/correlationids
Browse files Browse the repository at this point in the history
Display ResourceManager correlationIds in error output
  • Loading branch information
isra-fel authored Mar 11, 2020
2 parents 59d40fc + 26e2243 commit 5fe2f90
Show file tree
Hide file tree
Showing 38 changed files with 154 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.RestClients;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
using Microsoft.Rest.Azure;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -165,7 +166,7 @@ protected override void EndProcessing()
/// <summary>
/// The <c>ProcessRecord</c> method.
/// </summary>
public override void ExecuteCmdlet()
public sealed override void ExecuteCmdlet()
{
try
{
Expand Down Expand Up @@ -412,6 +413,25 @@ private void DisposeOfCancellationSource()
}
}

/// <summary>
/// Gets the source exception from a captured exception.
/// </summary>
/// <param name="capturedException">The captured exception</param>
private static Exception GetSourceException(ExceptionDispatchInfo capturedException)
{
if (capturedException.SourceException is AggregateException aggregateException)
{
var innerException = aggregateException.InnerExceptions?.SingleOrDefault();

if (innerException != null)
{
return innerException;
}
}

return capturedException.SourceException;
}

/// <summary>
/// Provides specialized exception handling.
/// </summary>
Expand All @@ -420,33 +440,14 @@ private void HandleException(ExceptionDispatchInfo capturedException)
{
try
{
var errorResponseException = capturedException.SourceException as ErrorResponseMessageException;
if (errorResponseException != null)
{
throw errorResponseException;
}
var sourceException = GetSourceException(capturedException);

var aggregateException = capturedException.SourceException as AggregateException;
if (aggregateException != null)
if (sourceException is CloudException cloudException)
{
if (aggregateException.InnerExceptions.CoalesceEnumerable().Any() &&
aggregateException.InnerExceptions.Count == 1)
{
errorResponseException = aggregateException.InnerExceptions.Single() as ErrorResponseMessageException;
if (errorResponseException != null)
{
throw errorResponseException;
}

throw aggregateException.InnerExceptions.Single();
}
else
{
throw aggregateException;
}
throw new ResourceManagerCloudException(cloudException);
}

throw capturedException.SourceException;
throw sourceException;
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class GetAzureManagementGroupDeploymentCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
FilterDeploymentOptions options = new FilterDeploymentOptions(DeploymentScopeType.ManagementGroup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class GetAzureManagementGroupDeploymentOperationCmdlet : ResourceManagerC
Mandatory = true, ValueFromPipeline = true, HelpMessage = "The deployment object.")]
public PSDeployment DeploymentObject { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var options = new FilterDeploymentOptions(DeploymentScopeType.ManagementGroup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class GetAzureSubscriptionDeploymentCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
FilterDeploymentOptions options = new FilterDeploymentOptions(DeploymentScopeType.Subscription)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class GetAzureSubscriptionDeploymentOperationCmdlet : ResourceManagerCmdl
ValueFromPipeline = true, HelpMessage = "The deployment object.")]
public PSDeployment DeploymentObject { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var deploymentName = !string.IsNullOrEmpty(this.DeploymentName) ? this.DeploymentName : this.DeploymentObject.DeploymentName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class GetAzureTenantDeploymentCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
FilterDeploymentOptions options = new FilterDeploymentOptions(DeploymentScopeType.Tenant)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class GetAzureTenantDeploymentOperationCmdlet : ResourceManagerCmdletBase
Mandatory = true, ValueFromPipeline = true, HelpMessage = "The deployment object.")]
public PSDeployment DeploymentObject { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var options = new FilterDeploymentOptions(DeploymentScopeType.Tenant)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class NewAzureManagementGroupDeploymentCmdlet : ResourceWithParameterCmdl
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var parameters = new PSDeploymentCmdletParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class NewAzureSubscriptionDeploymentCmdlet : ResourceWithParameterCmdletB
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var parameters = new PSDeploymentCmdletParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class NewAzureTenantDeploymentCmdlet : ResourceWithParameterCmdletBase, I
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var parameters = new PSDeploymentCmdletParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class RemoveAzureManagementGroupDeploymentCmdlet : ResourceManagerCmdletB
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
ConfirmAction(
ProjectResources.DeleteDeploymentMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class RemoveAzureSubscriptionDeploymentCmdlet : ResourceManagerCmdletBase
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
ConfirmAction(
ProjectResources.DeleteDeploymentMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class RemoveAzureTenantDeploymentCmdlet : ResourceManagerCmdletBase
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
ConfirmAction(
ProjectResources.DeleteDeploymentMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class StopAzureManagementGroupDeploymentCmdlet : ResourceManagerCmdletBas
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var options = new FilterDeploymentOptions(DeploymentScopeType.ManagementGroup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class StopAzureSubscriptionDeploymentCmdlet : ResourceManagerCmdletBase
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var options = new FilterDeploymentOptions(DeploymentScopeType.Subscription)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class StopAzureTenantDeploymentCmdlet : ResourceManagerCmdletBase
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var options = new FilterDeploymentOptions(DeploymentScopeType.Tenant)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class TestAzureManagementGroupDeploymentCmdlet : ResourceWithParameterCmd
[ValidateNotNullOrEmpty]
public string Location { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var parameters = new PSDeploymentCmdletParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class TestAzureSubscriptionDeploymentCmdlet : ResourceWithParameterCmdlet
[ValidateNotNullOrEmpty]
public string Location { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var parameters = new PSDeploymentCmdletParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class TestAzureTenantDeploymentCmdlet : ResourceWithParameterCmdletBase,
[ValidateNotNullOrEmpty]
public string Location { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var parameters = new PSDeploymentCmdletParameters()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class GetAzureLocationCmdlet : ResourceManagerCmdletBase
/// <summary>
/// Executes the cmdlet
/// </summary>
public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
if(DefaultContext.Subscription == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class GetAzurePolicyAlias : ResourceManagerCmdletBase
/// <summary>
/// Executes the cmdlet
/// </summary>
public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
// remove leading and trailing whitespace
this.NamespaceMatch = this.NamespaceMatch?.Trim() ?? string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class GetAzureProviderCmdlet : ResourceManagerCmdletBase
/// <summary>
/// Executes the cmdlet
/// </summary>
public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var providers = this.ListPSResourceProviders();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RegisterAzureProviderCmdlet : ResourceManagerCmdletBase
/// <summary>
/// Executes the cmdlet
/// </summary>
public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
this.ConfirmAction(
processMessage: ProjectResources.RegisterProviderMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class UnregisterAzureProviderCmdlet : ResourceManagerCmdletBase
/// <summary>
/// Executes the cmdlet
/// </summary>
public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
this.ConfirmAction(
processMessage: ProjectResources.UnregisterProviderMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class GetAzureResourceGroupDeploymentCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
FilterDeploymentOptions options = new FilterDeploymentOptions(DeploymentScopeType.ResourceGroup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public NewAzureResourceGroupDeploymentCmdlet()
this.Mode = DeploymentMode.Incremental;
}

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{

this.ConfirmAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class RemoveAzureResourceGroupDeploymentCmdlet : ResourceManagerCmdletBas
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
ConfirmAction(
ProjectResources.DeleteDeploymentMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class StopAzureResourceGroupDeploymentCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
var options = new FilterDeploymentOptions(DeploymentScopeType.ResourceGroup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public TestAzureResourceGroupDeploymentCmdlet()
this.Mode = DeploymentMode.Incremental;
}

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
if (RollbackToLastDeployment && !string.IsNullOrEmpty(RollBackDeploymentName))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class GetAzureResourceGroupCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public Hashtable Tag { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
Name = Name ?? ResourceIdentifier.FromResourceGroupIdentifier(this.Id).ResourceGroupName;
this.WriteObject(ResourceManagerSdkClient.FilterResourceGroups(name: this.Name, tag: this.Tag, detailed: false, location: this.Location), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class NewAzureResourceGroupCmdlet : ResourceManagerCmdletBase
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
public SwitchParameter Force { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
PSCreateResourceGroupParameters parameters = new PSCreateResourceGroupParameters
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class RemoveAzureResourceGroupCmdlet : ResourceManagerCmdletBase
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
Name = Name ?? ResourceIdentifier.FromResourceGroupIdentifier(this.Id).ResourceGroupName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class SetAzureResourceGroupCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
protected override void OnProcessRecord()
{
PSUpdateResourceGroupParameters parameters = new PSUpdateResourceGroupParameters
{
Expand Down
Loading

0 comments on commit 5fe2f90

Please sign in to comment.