Skip to content

Commit a8ded42

Browse files
authored
[Corehttp] Add model attribute to HttpResponseError (Azure#39636)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent 36cd31c commit a8ded42

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

sdk/core/corehttp/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added a new `AccessTokenInfo` class, which is returned by `get_token_info` implementations. This class contains the token, its expiration time, and optional additional information like when a token should be refreshed. [#38346](https://github.com/Azure/azure-sdk-for-python/pull/38346)
1010
- `BearerTokenCredentialPolicy` and `AsyncBearerTokenCredentialPolicy` now check if a credential has the `get_token_info` method defined. If so, the `get_token_info` method is used to acquire a token. [#38346](https://github.com/Azure/azure-sdk-for-python/pull/38346)
1111
- These policies now also check the `refresh_on` attribute when determining if a new token request should be made.
12+
- Added `model` attribute to `HttpResponseError` to allow accessing error attributes based on a known model. [#39636](https://github.com/Azure/azure-sdk-for-python/pull/39636)
1213

1314
### Breaking Changes
1415

sdk/core/corehttp/corehttp/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class HttpResponseError(BaseError):
187187
:vartype status_code: int
188188
:ivar response: The response that triggered the exception.
189189
:vartype response: ~corehttp.rest.HttpResponse or ~corehttp.rest.AsyncHttpResponse
190+
:ivar model: The response body model
191+
:vartype model: Any
190192
"""
191193

192194
def __init__(
@@ -200,6 +202,8 @@ def __init__(
200202
self.reason = response.reason
201203
self.status_code = response.status_code
202204

205+
self.model: Optional[Any] = kwargs.pop("model", None)
206+
203207
# By priority, message is:
204208
# - parameter "message", OR
205209
# - generic message using "reason"

sdk/core/corehttp/tests/test_exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ def test_httpresponse_error_with_response(self, port, transport):
7272
assert error.reason == "OK"
7373
assert isinstance(error.status_code, int)
7474

75+
@pytest.mark.parametrize("transport", SYNC_TRANSPORTS)
76+
def test_httpresponse_error_with_model(self, port, transport):
77+
request = HttpRequest("GET", url="http://localhost:{}/basic/string".format(port))
78+
client = MockRestClient(port, transport=transport())
79+
response = client.send_request(request, stream=False)
80+
error = HttpResponseError(response=response, model=FakeErrorTwo())
81+
assert error.message == "Operation returned an invalid status 'OK'"
82+
assert error.response is not None
83+
assert error.reason == "OK"
84+
assert error.model
85+
assert error.model.code == "FakeErrorTwo"
86+
assert error.model.message == "A different fake error"
87+
assert isinstance(error.status_code, int)
88+
7589
@pytest.mark.parametrize("transport", SYNC_TRANSPORTS)
7690
def test_malformed_json(self, port, transport):
7791
request = HttpRequest("GET", "/errors/malformed-json")

0 commit comments

Comments
 (0)