fix(http-client-python): raise azure-core error types for customized errors covering standard status codes#11250
fix(http-client-python): raise azure-core error types for customized errors covering standard status codes#11250l0lawrence wants to merge 2 commits into
Conversation
…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>
commit: |
|
All changed packages have been documented.
Show changes
|
|
You can try these changes here
|
|
Diff is Azure/azure-sdk-for-python#48056 |
msyyc
left a comment
There was a problem hiding this comment.
We ever discussed about it in teams channel and the breaking of spector case could be accepted.
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:
4XX) covering a standard code → fell through to a genericHttpResponseError.HttpResponseErrorfor 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 → ClientAuthenticationError404 → ResourceNotFoundError409 → ResourceExistsError304 → ResourceNotModifiedErrorBecause
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 genericHttpResponseErrorthat 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
404model), the dedicated error is still raised, but the deserialized custom body is no longer attached to it —map_errorraises before deserialization, soexc.modelisNonefor those codes. The body remains available onHttpResponseErrorfor non-standard codes. Theresponse-status-code-rangemock-api tests were updated to reflect this.Example generated output (ranged custom error + default custom error)
with the shared error map:
Validation
response-status-code-rangemock-api tests pass (sync + async, 4/4), exercising both an in-range code and the single 404 →ResourceNotFoundError.fixchangelog entry for@typespec/http-client-python.