|
2 | 2 |
|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
| 5 | +from typing import TYPE_CHECKING, Any, Optional, cast |
5 | 6 | from typing_extensions import Literal
|
6 | 7 |
|
7 | 8 | import httpx
|
8 | 9 |
|
| 10 | +from ._utils import is_dict |
| 11 | +from ._models import construct_type |
| 12 | +from .types.shared.error_code import ErrorCode |
| 13 | +from .types.shared.arbitrary_data import ArbitraryData |
| 14 | + |
9 | 15 | __all__ = [
|
10 | 16 | "BadRequestError",
|
11 | 17 | "AuthenticationError",
|
@@ -37,12 +43,35 @@ class APIError(GitpodError):
|
37 | 43 | If there was no response associated with this error then it will be `None`.
|
38 | 44 | """
|
39 | 45 |
|
40 |
| - def __init__(self, message: str, request: httpx.Request, *, body: object | None) -> None: # noqa: ARG002 |
| 46 | + code: Optional[ErrorCode] = None |
| 47 | + """ |
| 48 | + The status code, which should be an enum value of |
| 49 | + [google.rpc.Code][google.rpc.Code]. |
| 50 | + """ |
| 51 | + detail: Optional[ArbitraryData] = None |
| 52 | + """ |
| 53 | + Contains an arbitrary serialized message along with a @type that describes the |
| 54 | + type of the serialized message. |
| 55 | + """ |
| 56 | + if TYPE_CHECKING: |
| 57 | + # Stub to indicate that arbitrary properties are accepted. |
| 58 | + # To access properties that are not valid identifiers you can use `getattr`, e.g. |
| 59 | + # `getattr(obj, '$type')` |
| 60 | + def __getattr__(self, attr: str) -> object: ... |
| 61 | + |
| 62 | + def __init__(self, message: str, request: httpx.Request, *, body: object | None) -> None: |
41 | 63 | super().__init__(message)
|
42 | 64 | self.request = request
|
43 | 65 | self.message = message
|
44 | 66 | self.body = body
|
45 | 67 |
|
| 68 | + if is_dict(body): |
| 69 | + self.code = cast(Any, construct_type(type_=Optional[ErrorCode], value=body.get("code"))) |
| 70 | + self.detail = cast(Any, construct_type(type_=Optional[ArbitraryData], value=body.get("detail"))) |
| 71 | + else: |
| 72 | + self.code = None |
| 73 | + self.detail = None |
| 74 | + |
46 | 75 |
|
47 | 76 | class APIResponseValidationError(APIError):
|
48 | 77 | response: httpx.Response
|
|
0 commit comments