Map a generated ``ApiException`` (raised by the generated ``*Api`` classes on a non-2xx response) to a typed :class:`E2AError`.
(exc: ApiException)
| 280 | |
| 281 | |
| 282 | def from_api_exception(exc: ApiException) -> E2AError: |
| 283 | """Map a generated ``ApiException`` (raised by the generated ``*Api`` classes on a |
| 284 | non-2xx response) to a typed :class:`E2AError`.""" |
| 285 | headers = _normalize_headers(getattr(exc, "headers", None)) |
| 286 | request_id = _header_get(headers, "x-request-id") |
| 287 | status = int(getattr(exc, "status", 0) or 0) |
| 288 | |
| 289 | code = "" |
| 290 | message = getattr(exc, "reason", None) or str(exc) |
| 291 | details: Any = None |
| 292 | env = _parse_envelope(getattr(exc, "data", None), getattr(exc, "body", None)) |
| 293 | if isinstance(env, dict): |
| 294 | error = env.get("error") |
| 295 | if isinstance(error, dict): |
| 296 | code = error.get("code") or "" |
| 297 | message = error.get("message") or message |
| 298 | details = error.get("details") |
| 299 | |
| 300 | return to_e2a_error( |
| 301 | status=status, |
| 302 | code=code, |
| 303 | message=message, |
| 304 | request_id=request_id, |
| 305 | details=details, |
| 306 | headers=headers, |
| 307 | cause=exc, |
| 308 | ) |
| 309 | |
| 310 | |
| 311 | def _normalize_headers(headers: Any) -> Optional[Mapping[str, str]]: |