Skip to content

feat: Extend the UnexpectedStatus exception to include the response's content #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _parse_response(

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Lis

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _parse_response(

return response_423
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Fil

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Tes

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.UNAUTHORIZED:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Uni

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
6 changes: 5 additions & 1 deletion end_to_end_tests/golden-record/my_test_api_client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
class UnexpectedStatus(Exception):
"""Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True"""

...
def __init__(self, status_code: int, content: bytes):
self.status_code = status_code
self.content = content

super().__init__(f"Unexpected status code: {status_code}")


__all__ = ["UnexpectedStatus"]
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _parse_response(

return response_400
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _parse_response(

return response_400
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down
6 changes: 5 additions & 1 deletion integration-tests/integration_tests/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
class UnexpectedStatus(Exception):
"""Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True"""

...
def __init__(self, status_code: int, content: bytes):
self.status_code = status_code
self.content = content

super().__init__(f"Unexpected status code: {status_code}")


__all__ = ["UnexpectedStatus"]
3 changes: 1 addition & 2 deletions openapi_python_client/templates/endpoint_module.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _parse_response(*, client: Client, response: httpx.Response) -> Optional[{{
{% endif %}
{% endfor %}
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None

Expand Down Expand Up @@ -142,4 +142,3 @@ async def asyncio(
{{ kwargs(endpoint) }}
)).parsed
{% endif %}

7 changes: 6 additions & 1 deletion openapi_python_client/templates/errors.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

class UnexpectedStatus(Exception):
""" Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True """
...

def __init__(self, status_code: int, content: bytes):
self.status_code = status_code
self.content = content

super().__init__(f"Unexpected status code: {status_code}")

__all__ = ["UnexpectedStatus"]