Skip to content

[Sql] Fix ErrorResponseException handling to surface descriptive error messages across all Az.Sql cmdlets#29410

Open
achyuth-ms wants to merge 4 commits intoAzure:mainfrom
achyuth-ms:exceptionloggingfix
Open

[Sql] Fix ErrorResponseException handling to surface descriptive error messages across all Az.Sql cmdlets#29410
achyuth-ms wants to merge 4 commits intoAzure:mainfrom
achyuth-ms:exceptionloggingfix

Conversation

@achyuth-ms
Copy link
Copy Markdown
Member

@achyuth-ms achyuth-ms commented Apr 15, 2026

Description

Mandatory Checklist

  • SHOULD update ChangeLog.md file(s) appropriately
    • Update src/{{SERVICE}}/{{SERVICE}}/ChangeLog.md.
      • A snippet outlining the change(s) made in the PR should be written under the ## Upcoming Release header in the past tense.
    • Should not change ChangeLog.md if no new release is required, such as fixing test case only.
  • SHOULD regenerate markdown help files if there is cmdlet API change. Instruction
  • SHOULD have proper test coverage for changes in pull request.
  • SHOULD NOT adjust version of module manually in pull request

Copilot AI review requested due to automatic review settings April 15, 2026 22:34
@azure-client-tools-bot-prd
Copy link
Copy Markdown

Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status.

@azure-pipelines
Copy link
Copy Markdown
Contributor

Azure Pipelines:
4 pipeline(s) require an authorized user to comment /azp run to run.

@achyuth-ms
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown
Contributor

Commenter does not have sufficient privileges for PR 29410 in repo Azure/azure-powershell

@achyuth-ms achyuth-ms marked this pull request as ready for review April 15, 2026 22:39
@azure-pipelines
Copy link
Copy Markdown
Contributor

Azure Pipelines:
4 pipeline(s) require an authorized user to comment /azp run to run.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ErrorResponseExceptionHelper and updated AzureSqlCmdletBase.ExecuteCmdlet() to wrap ErrorResponseException into an AzPSCloudException with 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.md and InternalsVisibleTo for 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.

Comment thread src/Sql/Sql/Common/ErrorResponseExceptionHelper.cs Outdated
Comment thread src/Sql/Sql/Common/ErrorResponseExceptionHelper.cs
Comment thread src/Sql/Sql/Server/Services/AzureSqlDeletedServerAdapter.cs
Comment thread src/Sql/Sql/Server/Services/AzureSqlDeletedServerAdapter.cs Outdated
@ericshape
Copy link
Copy Markdown
Member

/azp run

@azure-pipelines
Copy link
Copy Markdown
Contributor

Commenter does not have sufficient privileges for PR 29410 in repo Azure/azure-powershell

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Comment thread src/Sql/Sql/Common/ErrorResponseExceptionHelper.cs Outdated
Comment thread src/Sql/Sql/Common/ErrorResponseExceptionHelper.cs Outdated
@VeryEarly
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown
Contributor

Azure Pipelines successfully started running 3 pipeline(s).

@VeryEarly VeryEarly self-assigned this Apr 17, 2026
@achyuth-ms
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown
Contributor

Commenter does not have sufficient privileges for PR 29410 in repo Azure/azure-powershell

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Comment thread src/Sql/Sql/Common/ErrorResponseExceptionHelper.cs Outdated
Comment thread src/Sql/Sql/ChangeLog.md Outdated
- 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.
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/Sql/Sql/Server/Services/AzureSqlDeletedServerAdapter.cs Outdated
Comment on lines +93 to +97
{
wrappedException.Data["Request"] = ex.Request;
}
if (ex.Response != null)
{
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Suggested change
{
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;

Copilot uses AI. Check for mistakes.
Comment thread src/Sql/Sql/Common/ErrorResponseExceptionHelper.cs Outdated
@VeryEarly
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown
Contributor

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants