Raised when an API response has a status code of 4xx or 5xx.
| 83 | |
| 84 | |
| 85 | class APIStatusError(APIError): |
| 86 | """Raised when an API response has a status code of 4xx or 5xx.""" |
| 87 | |
| 88 | response: httpx2.Response |
| 89 | |
| 90 | status_code: int |
| 91 | |
| 92 | request_id: str | None |
| 93 | |
| 94 | workspace_id: str | None |
| 95 | |
| 96 | api_message: str | None |
| 97 | """A human-readable explanation of the error. |
| 98 | |
| 99 | Its wording can change, so don't match on it. |
| 100 | |
| 101 | `error.message` from the response body, if present. |
| 102 | """ |
| 103 | |
| 104 | type: ErrorType | None |
| 105 | """The broad category of the error. |
| 106 | |
| 107 | `error.type` from the response body, if present. |
| 108 | """ |
| 109 | |
| 110 | error_code: str | None |
| 111 | """A stable code that names the cause. |
| 112 | |
| 113 | `error.details.error_code` from the response body, if present and a string. |
| 114 | """ |
| 115 | |
| 116 | details: ErrorDetails | None |
| 117 | """Machine-readable detail about the cause of the error. |
| 118 | |
| 119 | `error_code` names the cause; branch on it rather than on `message`. Absent when |
| 120 | the error has no code. Treat an unrecognized `error_code` as the bare `type`. |
| 121 | |
| 122 | `error.details` from the response body, if present. |
| 123 | """ |
| 124 | |
| 125 | def __init__(self, message: str, *, response: httpx2.Response, body: object | None) -> None: |
| 126 | # imported here so that modules in `types` can import this one |
| 127 | from .types.shared.error_details import ErrorDetails |
| 128 | |
| 129 | super().__init__(message, response.request, body=body) |
| 130 | self.response = response |
| 131 | self.status_code = response.status_code |
| 132 | self.request_id = response.headers.get("request-id") |
| 133 | self.workspace_id = response.headers.get("anthropic-workspace-id") |
| 134 | self.api_message = extract_property(body, "error", "message") |
| 135 | self.type = extract_property(body, "error", "type") |
| 136 | self.error_code = extract_error_code(body) |
| 137 | self.details = construct_type_or_none( |
| 138 | value=extract_property(body, "error", "details"), type_=cast(Any, ErrorDetails) |
| 139 | ) |
| 140 | |
| 141 | |
| 142 | class APIConnectionError(APIError): |
no outgoing calls