Skip to content

Commit 70975a7

Browse files
committed
- Calling custom HTTP Exception on request failure
1 parent aaaaaa6 commit 70975a7

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/openapi_python_generator/language_converters/python/templates/apiconfig.jinja2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ class APIConfig():
2626
{% else %}
2727
_access_token = value
2828
{% endif %}
29+
30+
class HTTPException(Exception):
31+
def __init__(self, status_code: int, message: str):
32+
self.status_code = status_code
33+
self.message = message
34+
super().__init__(f"{status_code} {message}")
35+
36+
def __str__(self):
37+
return f"{self.status_code} {self.message}"

src/openapi_python_generator/language_converters/python/templates/httpx.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ with httpx.Client(base_url=base_path) as client:
2929
)
3030

3131
if response.status_code != {{ return_type.status_code }}:
32-
raise Exception(f'{{ operationId }} failed with status code: {response.status_code}')
32+
raise HTTPException(response.status_code, f'{{ operationId }} failed with status code: {response.status_code}')
3333
{% if return_type.complex_type %}
3434
{% if return_type.list_type is none %}
3535
return {{ return_type.type.converted_type }}(**response.json())

src/openapi_python_generator/language_converters/python/templates/requests.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def {{ operation_id }}({{ params }}) -> {% if return_type.type is none or return
2222
{% endif %}
2323
)
2424
if response.status_code != {{ return_type.status_code }}:
25-
raise Exception(f'{{ operationId }} failed with status code: {response.status_code}')
25+
raise HTTPException(response.status_code, f'{{ operationId }} failed with status code: {response.status_code}')
2626
{% if return_type.complex_type %}
2727
{% if return_type.list_type is none %}
2828
return {{ return_type.type.converted_type }}(**response.json())

src/openapi_python_generator/language_converters/python/templates/service.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import {{ library_import }}
33
import json
44

55
from ..models import *
6-
from ..api_config import APIConfig
6+
from ..api_config import APIConfig, HTTPException
77

88
{{ content | safe}}

0 commit comments

Comments
 (0)