Skip to content

fix(http-client-python): raise azure-core error types for customized errors covering standard status codes - #11250

Merged
iscai-msft merged 4 commits into
mainfrom
python-custom-error-status-mapping-upstream
Aug 27, 2026
Merged

iscai-msft merged 4 commits into
mainfrom
python-custom-error-status-mapping-upstream

Conversation

@l0lawrence

Copy link
Copy Markdown
Member

Problem

When a TypeSpec operation declares a customized error model that covers one of the standard azure-core status codes (401, 404, 409, 304), the Python emitter did not consistently raise the dedicated azure-core error type:

  • Ranged custom error (e.g. 4XX) covering a standard code → fell through to a generic HttpResponseError.
  • Default custom error model (covers all non-success codes) → likewise fell through to HttpResponseError for the standard codes.

Only a custom error targeting a standard code specifically raised the dedicated type, so behavior was inconsistent across the single / ranged / default cases.

Fix

Standard status codes now always map to their dedicated azure-core error type via the operation error_map, regardless of whether a customized error model (single, ranged, or default) also covers them:

  • 401 → ClientAuthenticationError
  • 404 → ResourceNotFoundError
  • 409 → ResourceExistsError
  • 304 → ResourceNotModifiedError

Because map_error(status_code=..., response=..., error_map=error_map) runs first in the generated handler, these codes raise their dedicated type there. The customized error body continues to be deserialized and attached (model=error) to the generic HttpResponseError that is raised for any other (non-standard) status codes covered by the customized error.

Behavior change note

For a custom error that targets a standard code specifically (e.g. a 404 model), the dedicated error is still raised, but the deserialized custom body is no longer attached to it — map_error raises before deserialization, so exc.model is None for those codes. The body remains available on HttpResponseError for non-standard codes. The response-status-code-range mock-api tests were updated to reflect this.

Example generated output (ranged custom error + default custom error)

if response.status_code not in [204]:
    map_error(status_code=response.status_code, response=response, error_map=error_map)
    error = None
    if 494 <= response.status_code <= 499:
        error = _failsafe_deserialize(_models.ErrorInRange, response)
    else:
        error = _failsafe_deserialize(_models.DefaultError, response)
    raise HttpResponseError(response=response, model=error)

with the shared error map:

error_map: MutableMapping = {
    401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError,
    304: ResourceNotModifiedError,
}

Validation

  • Regenerated all Azure + unbranded spector specs (181 specs, 0 failures).
  • response-status-code-range mock-api tests pass (sync + async, 4/4), exercising both an in-range code and the single 404 → ResourceNotFoundError.
  • Added a fix changelog entry for @typespec/http-client-python.

…errors covering standard status codes

Always populate the operation error_map with the standard azure-core error types (401 -> ClientAuthenticationError, 404 -> ResourceNotFoundError, 409 -> ResourceExistsError, 304 -> ResourceNotModifiedError), even when a customized error model covers those status codes via a ranged or default error response. map_error then raises the dedicated error type instead of falling back to a generic HttpResponseError. The customized error body continues to be deserialized and attached to the HttpResponseError raised for other (non-standard) status codes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@11250

commit: 0431ed3

@github-actions

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • ✅ @typespec/http-client-python
Show changes

@typespec/http-client-python - fix ✏️

Always populate the operation error_map with the standard azure-core error types (401 → ClientAuthenticationError, 404 → ResourceNotFoundError, 409 → ResourceExistsError, 304 → ResourceNotModifiedError), even when a customized error model covers those status codes. Previously, a standard status code covered by a customized ranged or default error model fell back to a generic HttpResponseError; it now raises its dedicated error type via map_error. The customized error body continues to be deserialized and attached to the HttpResponseError raised for other (non-standard) status codes.

@azure-sdk-automation

azure-sdk-automation Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@l0lawrence

Copy link
Copy Markdown
Member Author

Diff is Azure/azure-sdk-for-python#48056

@msyyc Yuchao Yan (msyyc) 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.

We ever discussed about it in teams channel and the breaking of spector case could be accepted.

@msyyc

Copy link
Copy Markdown
Contributor

FYI: the CI failure of Python Integration / Mock API Tests (pull_request) is caused by the breaking for test case packages/http-client-python/tests/mock_api/shared/test_response_status_code_range.py which is also in typespec-azure.

To fix the failure, we will have to create a PR in typespec-azure to skip the test case; then rerun pipeline. After this PR merged, release a new version of http-client-python. Then bump the new version in typespec-azure and reopen the skipped test case.

NOTE: it is very annoying for such kind of ci failure and the root cause is that we put source code in microsoft repo but put ci check in typespec-azure repo. I made a proposal in e-mail Re: Proposal — Swap the typespec-python ↔ http-client-python dependency (short-term fix for the circular dependency) which could avoid such annoying CI failure in the future. And you could express your opinion in the e-mail. Libba Lawrence (@l0lawrence)

@l0lawrence

Copy link
Copy Markdown
Member Author

holding off on this pr until the typeddict fixes get in

@microsoft-github-policy-service microsoft-github-policy-service Bot added the stale Mark a PR that hasn't been recently updated and will be closed. label Aug 11, 2026
@l0lawrence Libba Lawrence (l0lawrence) removed the stale Mark a PR that hasn't been recently updated and will be closed. label Aug 11, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added the stale Mark a PR that hasn't been recently updated and will be closed. label Aug 26, 2026
iscai-msft and others added 2 commits August 26, 2026 11:59
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Resolve the error map conflict by preserving dedicated azure-core mappings for standard status codes covered by customized errors.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 26, 2026 16:06

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 updates the @typespec/http-client-python emitter so that operations always raise the dedicated azure-core exception types for standard status codes (401/404/409/304) via map_error and the per-operation error_map, even when custom error models (single, ranged, or default) also cover those codes.

Changes:

  • Always populate the generated operation error_map with the standard azure-core exception mappings so map_error raises the dedicated types consistently.
  • Remove generator-side “raise built-in error type” branching in custom-error handling and rely on map_error for standard codes.
  • Update the sync/async response-status-code-range mock-api tests and add a Chronus fix entry.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py Always emits standard-code azure-core mappings in error_map; adjusts custom-error deserialization logic to assume map_error handles standard codes.
packages/http-client-python/tests/mock_api/shared/test_response_status_code_range.py Updates 404 test expectations to reflect ResourceNotFoundError raised by map_error.
packages/http-client-python/tests/mock_api/shared/asynctests/test_response_status_code_range_async.py Async equivalent of the 404 test update.
.chronus/changes/python-custom-error-status-mapping-2026-6-16-9-52-50.md Adds a changelog entry documenting the behavior change.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

Copy link
Copy Markdown
Contributor

Python emitter diff

Baseline gh:210f84c0dee6cfb8226ce95700562c9bf0922822 vs this PR.

Diff summary: 4 file(s), +16 / -8

Rendered diff: inline on the run summary, or the emitter-diff-html artifact.

Informational check (eng/emitter-diff); does not block the PR.

@iscai-msft
iscai-msft added this pull request to the merge queue Aug 27, 2026
Merged via the queue into main with commit 806e1ab Aug 27, 2026
46 of 47 checks passed
@iscai-msft
iscai-msft deleted the python-custom-error-status-mapping-upstream branch August 27, 2026 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python stale Mark a PR that hasn't been recently updated and will be closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants