Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.27k stars 3.87k forks source link

ManagementGroup cmdlets do not pass InnerException back to caller #15938

Open kwill-MSFT opened 3 years ago

kwill-MSFT commented 3 years ago

Description of the new feature

All other Az Powershell cmdlets return the details of the REST API response in the error's InnerException property whenever an error is encountered. This makes it possible to do things such as:

try
{
   Set-AzNetworkSecurityGroup ....
}
catch
{
   if ($_.Exception.InnerException.Body.Code -eq "ResourceGroupNotFound")
   ...
   elseif ($_.Exception.InnerException.Body.Code -in @("Canceled", "CanceledAndSupersededDueToAnotherOperation"))
   ...
}

But for the Management Group cmdlets such as New-AzManagementGroupSubscription, only the error message is returned to the caller so it isn't possible to query for things such as the code or details.

This is done in https://github.com/Azure/azure-powershell/blob/main/src/Resources/Resources/ManagementGroups/Common/Utility.cs with:

                if (content.ContainsKey("Message"))
                {
                    throw new CloudException(content["Message"].ToString());
                }

This should be changed to return the full error details the same way other Az cmdlets do.

Proposed implementation details (optional)

ghost commented 3 years ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @armleads-azure.

Issue Details
## Description of the new feature All other Az Powershell cmdlets return the details of the REST API response in the error's InnerException property whenever an error is encountered. This makes it possible to do things such as: ``` try { Set-AzNetworkSecurityGroup .... } catch { if ($_.Exception.InnerException.Body.Code -eq "ResourceGroupNotFound") ... elseif ($_.Exception.InnerException.Body.Code -in @("Canceled", "CanceledAndSupersededDueToAnotherOperation")) ... } ``` But for the Management Group cmdlets such as New-AzManagementGroupSubscription, only the error message is returned to the caller so it isn't possible to query for things such as the code or details. This is done in https://github.com/Azure/azure-powershell/blob/main/src/Resources/Resources/ManagementGroups/Common/Utility.cs with: ``` if (content.ContainsKey("Message")) { throw new CloudException(content["Message"].ToString()); } ``` This should be changed to return the full error details the same way other Az cmdlets do. ## Proposed implementation details (optional)
Author: kwill-MSFT
Assignees: -
Labels: `feature-request`, `Service Attention`, `ARM - Core`
Milestone: -