[Sql] Fix ErrorResponseException handling to surface descriptive error messages across all Az.Sql cmdlets#29410
[Sql] Fix ErrorResponseException handling to surface descriptive error messages across all Az.Sql cmdlets#29410achyuth-ms wants to merge 4 commits intoAzure:mainfrom
Conversation
| Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status. |
|
Azure Pipelines: 4 pipeline(s) require an authorized user to comment /azp run to run. |
|
/azp run |
|
Commenter does not have sufficient privileges for PR 29410 in repo Azure/azure-powershell |
|
Azure Pipelines: 4 pipeline(s) require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR aims to improve Az.Sql user experience by ensuring ErrorResponseException failures surface descriptive REST error messages (for example, Azure Policy violation details) instead of generic SDK exception text, primarily by centralizing exception rewriting in the Sql cmdlet base.
Changes:
- Added
ErrorResponseExceptionHelperand updatedAzureSqlCmdletBase.ExecuteCmdlet()to wrapErrorResponseExceptioninto anAzPSCloudExceptionwith a more descriptive message. - Refactored a few adapters (deleted server, managed instance link) to rely on the centralized error handling and removed local “message improvement” helpers.
- Added unit tests for the new helper and updated Sql
ChangeLog.mdandInternalsVisibleTofor the new test assembly.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Sql/Sql/Common/ErrorResponseExceptionHelper.cs | New helper to extract detailed error messages from ErrorResponseException body/response content. |
| src/Sql/Sql/Common/AzureSqlCmdletBase.cs | Wraps ErrorResponseException in a centralized catch to ensure descriptive messages for cmdlets using this base. |
| src/Sql/Sql/Server/Services/AzureSqlDeletedServerAdapter.cs | Special-cases NotFound for deleted server lookup; otherwise uses the new helper. |
| src/Sql/Sql/DistributedAvailabilityGroup/Services/AzureSqlManagedInstanceLinkAdapter.cs | Removes local exception-rewrite method and rethrows to allow centralized handling. |
| src/Sql/Sql/Properties/AssemblyInfo.cs | Adds InternalsVisibleTo for the Sql test assembly. |
| src/Sql/Sql.Test/UnitTests/ErrorResponseExceptionHelperTests.cs | New unit tests validating message extraction precedence and parsing behavior. |
| src/Sql/Sql/ChangeLog.md | Adds an Upcoming Release note describing the improved error handling behavior. |
|
/azp run |
|
Commenter does not have sufficient privileges for PR 29410 in repo Azure/azure-powershell |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
/azp run |
|
Commenter does not have sufficient privileges for PR 29410 in repo Azure/azure-powershell |
| - Additional information about change #1 | ||
| --> | ||
| ## Upcoming Release | ||
| * Fixed error handling across all Az.Sql cmdlets to surface descriptive error messages instead of generic 'Operation returned an invalid status code' when API calls fail. This restores meaningful error details such as Azure Policy violation messages. |
There was a problem hiding this comment.
The changelog entry claims the fix applies "across all Az.Sql cmdlets", but there are Az.Sql cmdlets that do not go through AzureSqlCmdletBase (e.g., some inherit directly from AzureRMCmdlet). Consider rewording this entry to accurately scope the change to cmdlets impacted by ErrorResponseException message propagation.
| { | ||
| wrappedException.Data["Request"] = ex.Request; | ||
| } | ||
| if (ex.Response != null) | ||
| { |
There was a problem hiding this comment.
ErrorResponseExceptionHelper is storing request/response objects only in Exception.Data. For consistency with other cmdlets that wrap into AzPSCloudException (and to ensure standard formatting/telemetry continues to work), set the AzPSCloudException.Request and .Response properties directly instead of (or in addition to) custom Data keys like "Request"/"Response".
| { | |
| wrappedException.Data["Request"] = ex.Request; | |
| } | |
| if (ex.Response != null) | |
| { | |
| { | |
| wrappedException.Request = ex.Request; | |
| wrappedException.Data["Request"] = ex.Request; | |
| } | |
| if (ex.Response != null) | |
| { | |
| wrappedException.Response = ex.Response; |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
Description
Mandatory Checklist
Please choose the target release of Azure PowerShell. (⚠️ Target release is a different concept from API readiness. Please click below links for details.)
Check this box to confirm: I have read the Submitting Changes section of
CONTRIBUTING.mdand reviewed the following information:ChangeLog.mdfile(s) appropriatelysrc/{{SERVICE}}/{{SERVICE}}/ChangeLog.md.## Upcoming Releaseheader in the past tense.ChangeLog.mdif no new release is required, such as fixing test case only.