|
1 | | -from typing import Any, Optional |
| 1 | +from typing import Any, Dict, Optional |
2 | 2 |
|
3 | | -from typing_extensions import Self |
4 | 3 |
|
5 | | -import zitadel_client.rest_response |
| 4 | +class ZitadelError(Exception): |
| 5 | + """Base exception for all Zitadel API errors.""" |
6 | 6 |
|
7 | 7 |
|
8 | | -class OpenApiError(Exception): |
9 | | - """The base exception class for all OpenApiErrors""" |
| 8 | +class ApiError(ZitadelError): |
| 9 | + """ |
| 10 | + Represents an HTTP error from the Zitadel API. |
10 | 11 |
|
| 12 | + Attributes: |
| 13 | + code int HTTP status code |
| 14 | + response_headers Dict[str, str] HTTP response headers |
| 15 | + response_body Any HTTP response body |
| 16 | + """ |
11 | 17 |
|
12 | | -class ApiException(OpenApiError): # noqa N818 should be named with an Error suffix later |
13 | 18 | def __init__( |
14 | 19 | self, |
15 | | - status: Optional[int] = None, |
16 | | - reason: Optional[str] = None, |
17 | | - http_resp: Optional[zitadel_client.rest_response.RESTResponse] = None, |
18 | | - *, |
19 | | - body: Optional[str] = None, |
20 | | - data: Optional[Any] = None, |
| 20 | + code: int, |
| 21 | + response_headers: Dict[str, str], |
| 22 | + response_body: Optional[Any], |
21 | 23 | ) -> None: |
22 | | - self.status = status |
23 | | - self.reason = reason |
24 | | - self.body = body |
25 | | - self.data = data |
26 | | - self.headers = None |
27 | | - |
28 | | - if http_resp: |
29 | | - if self.status is None: |
30 | | - self.status = http_resp.status |
31 | | - if self.reason is None: |
32 | | - self.reason = http_resp.reason |
33 | | - if self.body is None and http_resp.data is not None: |
34 | | - # noinspection PyBroadException |
35 | | - try: |
36 | | - self.body = http_resp.data.decode("utf-8") |
37 | | - except Exception: # noqa: S110 |
38 | | - pass |
39 | | - self.headers = http_resp.getheaders() |
40 | | - |
41 | | - @classmethod |
42 | | - def from_response( |
43 | | - cls, |
44 | | - *, |
45 | | - http_resp: zitadel_client.rest_response.RESTResponse, |
46 | | - body: Optional[str], |
47 | | - data: Optional[Any], |
48 | | - ) -> Self: |
49 | | - if http_resp.status == 400: |
50 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
51 | | - |
52 | | - if http_resp.status == 401: |
53 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
54 | | - |
55 | | - if http_resp.status == 403: |
56 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
57 | | - |
58 | | - if http_resp.status == 404: |
59 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
60 | | - |
61 | | - # Added new conditions for 409 and 422 |
62 | | - if http_resp.status == 409: |
63 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
64 | | - |
65 | | - if http_resp.status == 422: |
66 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
67 | | - |
68 | | - if 500 <= http_resp.status <= 599: |
69 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
70 | | - raise ApiException(http_resp=http_resp, body=body, data=data) |
71 | | - |
72 | | - def __str__(self) -> str: |
73 | | - """Custom error messages for exception""" |
74 | | - error_message = "({0})\nReason: {1}\n".format(self.status, self.reason) |
75 | | - if self.headers: |
76 | | - error_message += "HTTP response headers: {0}\n".format(self.headers) |
77 | | - |
78 | | - if self.data or self.body: |
79 | | - error_message += "HTTP response body: {0}\n".format(self.data or self.body) |
80 | | - |
81 | | - return error_message |
| 24 | + super().__init__(f"Error {code}") |
| 25 | + self.code = code |
| 26 | + self.response_headers = response_headers |
| 27 | + self.response_body = response_body |
0 commit comments