Fix New-AzRoleAssignment error message to include service error details#29466
Merged
VeryEarly merged 5 commits intoApr 29, 2026
Conversation
| Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves user-facing error messages in the Az.Resources authorization cmdlets by extracting and surfacing service-provided error details (code/message) from ErrorResponseException, rather than only showing generic HTTP status-code exceptions.
Changes:
- Added
AuthorizationErrorResponseExceptionHelperto convert Authorization SDKErrorResponseExceptionintoAzPSCloudExceptionwith more descriptive messages. - Updated role assignment/definition flows in
AuthorizationClientto catchErrorResponseExceptionand rethrow enriched exceptions (including fixing a dead Hyak exception handler). - Added unit tests for the helper and updated
ChangeLog.mdwith the user-visible behavior change and issue references.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Resources/Resources/Models.Authorization/AuthorizationClient.cs | Wraps specific Authorization SDK calls to rethrow enriched, user-friendly exceptions; replaces obsolete Hyak exception handling. |
| src/Resources/Resources/Helper/AuthorizationErrorResponseExceptionHelper.cs | New helper to extract error details from ErrorResponseException and build an AzPSCloudException. |
| src/Resources/Resources/ChangeLog.md | Documents the improved error messages and references issues [#19605] and [#19374]. |
| src/Resources/Resources.Test/UnitTests/AuthorizationErrorResponseExceptionHelperTests.cs | Adds unit tests validating the helper’s message shaping and preservation of request/response. |
Member
|
Also, can you update the PR title? |
Member
Author
|
@microsoft-github-policy-service agree company="Microsoft" |
VeryEarly
approved these changes
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes error message clarity issues in role assignment and role definition cmdlets by surfacing service error details that were previously buried in exception properties.
Fixes #19605:
New-AzRoleAssignmentandSet-AzRoleAssignmentnow display descriptive error messages (e.g., "RoleAssignmentExists: The role assignment already exists") instead of generic HTTP status codes.Fixes #19374:
New-AzRoleDefinitionandSet-AzRoleDefinitionnow surface service error details for validation failures and permission errors.Additional fix: Replaced dead
Hyak.Common.CloudExceptionexception handler inCreateOrUpdateRoleDefinitionthat prevented the friendly "TenantNotAllowed" message from appearing since the SDK migration from Hyak to AutoRest.Root Cause
The SDK-generated
ErrorResponseExceptiononly includes HTTP status codes in its message (e.g.,"Operation returned an invalid status code 'Conflict'"). The actual service error details (error code and message) are buried inex.Body.Errororex.Response.Content, making it difficult for users to diagnose issues.Changes:
AuthorizationErrorResponseExceptionHelperinsrc/Resources/Resources/Helper/to extract error details fromErrorResponseExceptionand wrap them inAzPSCloudExceptionwith both user-facing and telemetry-safe messagesCreateRoleAssignment(used byNew-AzRoleAssignment)UpdateRoleAssignment(used bySet-AzRoleAssignment)FilterRoleDefinitions(used internally when-RoleDefinitionNameis specified in role assignment cmdlets)CreateOrUpdateRoleDefinition(used byNew-AzRoleDefinitionandSet-AzRoleDefinition)AuthorizationErrorResponseExceptionHelperTests.cscovering all error extraction code pathsChangeLog.mdExample - Before this PR:
PS> New-AzRoleAssignment -ObjectId $principalId -RoleDefinitionName "Reader" -Scope "/subscriptions/bad-guid" New-AzRoleAssignment: Operation returned an invalid status code 'Conflict'. RoleAssignmentExists: The role assignment already exists.
Testing
Mandatory Checklist
Please choose the target release of Azure PowerShell.
Check this box to confirm: I have read the Submitting Changes section of
CONTRIBUTING.mdand reviewed the following information:ChangeLog.mdfile(s) appropriatelysrc/Resources/Resources/ChangeLog.mdunder## Upcoming Releaseheader with description of error message improvementsAuthorizationErrorResponseExceptionHelperTests.cswith 4 unit tests covering all code paths