Skip to content

Fix New-AzRoleAssignment error message to include service error details#29466

Merged
VeryEarly merged 5 commits into
Azure:release-2026-05-05from
MaddyMicrosoft:fix/issue-19605-roleassignment-error-message
Apr 29, 2026
Merged

Fix New-AzRoleAssignment error message to include service error details#29466
VeryEarly merged 5 commits into
Azure:release-2026-05-05from
MaddyMicrosoft:fix/issue-19605-roleassignment-error-message

Conversation

@MaddyMicrosoft

Copy link
Copy Markdown
Member

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-AzRoleAssignment and Set-AzRoleAssignment now display descriptive error messages (e.g., "RoleAssignmentExists: The role assignment already exists") instead of generic HTTP status codes.

Fixes #19374: New-AzRoleDefinition and Set-AzRoleDefinition now surface service error details for validation failures and permission errors.

Additional fix: Replaced dead Hyak.Common.CloudException exception handler in CreateOrUpdateRoleDefinition that prevented the friendly "TenantNotAllowed" message from appearing since the SDK migration from Hyak to AutoRest.

Root Cause

The SDK-generated ErrorResponseException only 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 in ex.Body.Error or ex.Response.Content, making it difficult for users to diagnose issues.

Changes:

  • Created AuthorizationErrorResponseExceptionHelper in src/Resources/Resources/Helper/ to extract error details from ErrorResponseException and wrap them in AzPSCloudException with both user-facing and telemetry-safe messages
  • Applied error enrichment to:
    • CreateRoleAssignment (used by New-AzRoleAssignment)
    • UpdateRoleAssignment (used by Set-AzRoleAssignment)
    • FilterRoleDefinitions (used internally when -RoleDefinitionName is specified in role assignment cmdlets)
    • CreateOrUpdateRoleDefinition (used by New-AzRoleDefinition and Set-AzRoleDefinition)
  • Added 4 unit tests in AuthorizationErrorResponseExceptionHelperTests.cs covering all error extraction code paths
  • Telemetry uses desensitized error codes (PII-safe per GDPR requirements)
  • Updated ChangeLog.md

Example - 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

  • Added 4 unit tests (all passing)
  • Manually validated against live Azure subscription with various error scenarios
  • Existing 27 role assignment scenario tests pass
  • Build and static analysis pass

Mandatory Checklist

  • SHOULD update ChangeLog.md file(s) appropriately
    • Updated src/Resources/Resources/ChangeLog.md under ## Upcoming Release header with description of error message improvements
  • SHOULD regenerate markdown help files if there is cmdlet API change. Instruction
    • Not applicable - no cmdlet API changes, error message improvements only
  • SHOULD have proper test coverage for changes in pull request.
    • Added AuthorizationErrorResponseExceptionHelperTests.cs with 4 unit tests covering all code paths
    • Manually validated error scenarios against live Azure subscription
    • Existing 27 scenario tests for role assignments continue to pass
  • SHOULD NOT adjust version of module manually in pull request
    • No version changes made

Copilot AI review requested due to automatic review settings April 28, 2026 00:46
@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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 AuthorizationErrorResponseExceptionHelper to convert Authorization SDK ErrorResponseException into AzPSCloudException with more descriptive messages.
  • Updated role assignment/definition flows in AuthorizationClient to catch ErrorResponseException and rethrow enriched exceptions (including fixing a dead Hyak exception handler).
  • Added unit tests for the helper and updated ChangeLog.md with 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.

Comment thread src/Resources/Resources/Models.Authorization/AuthorizationClient.cs Outdated
Comment thread src/Resources/Resources/Helper/AuthorizationErrorResponseExceptionHelper.cs Outdated
@MaddyMicrosoft MaddyMicrosoft added this to the Az 16.0.0 (06/02/2026) milestone Apr 28, 2026
@notyashhh

Copy link
Copy Markdown
Member

Also, can you update the PR title?

@MaddyMicrosoft

Copy link
Copy Markdown
Member Author

@microsoft-github-policy-service agree company="Microsoft"

Copilot AI review requested due to automatic review settings April 29, 2026 01:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread src/Resources/Resources/Helper/AuthorizationErrorResponseExceptionHelper.cs Outdated
Comment thread src/Resources/Resources/Models.Authorization/AuthorizationClient.cs
@MaddyMicrosoft MaddyMicrosoft changed the title Fix/issue 19605 roleassignment error message Fix New-AzRoleAssignment error message to include service error details Apr 29, 2026
@VeryEarly VeryEarly changed the base branch from main to release-2026-05-05 April 29, 2026 06:15
@VeryEarly VeryEarly enabled auto-merge (squash) April 29, 2026 06:17
@VeryEarly VeryEarly disabled auto-merge April 29, 2026 07:57
@VeryEarly VeryEarly merged commit c5b53f1 into Azure:release-2026-05-05 Apr 29, 2026
6 of 12 checks passed
@MaddyMicrosoft MaddyMicrosoft deleted the fix/issue-19605-roleassignment-error-message branch June 9, 2026 03:06
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.

New-AzRoleAssignment doesn't return error other than status code New-AzRoleAssignment returned an invalid status code 'BadRequest'

4 participants