|
8 | 8 | from sqlite3 import Error as SqliteError |
9 | 9 | from typing import Any, Optional, Protocol, TypeVar, Union |
10 | 10 |
|
| 11 | +import httpx |
11 | 12 | import jwt |
12 | 13 | from asyncpg import exceptions as postgres_exceptions |
13 | 14 | from pydantic import ValidationError as PydanticValidationError |
@@ -67,9 +68,9 @@ def __init__(self, api_spec: ApiSpec, base: type[BaseRenderer] = TextRenderer) - |
67 | 68 | self.api_spec = api_spec |
68 | 69 | super().__init__(base) |
69 | 70 |
|
70 | | - def default(self, request: Request, exception: Exception) -> HTTPResponse: |
71 | | - """Overrides the default error handler.""" |
72 | | - formatted_exception = errors.BaseError() |
| 71 | + @classmethod |
| 72 | + def _get_formatted_exception(cls, request: Request, exception: Exception) -> errors.BaseError | None: |
| 73 | + formatted_exception: errors.BaseError | None = None |
73 | 74 | match exception: |
74 | 75 | case errors.BaseError(): |
75 | 76 | formatted_exception = exception |
@@ -136,6 +137,24 @@ def default(self, request: Request, exception: Exception) -> HTTPResponse: |
136 | 137 | case CancelledError(): |
137 | 138 | quiet = request.transport.is_closing() |
138 | 139 | formatted_exception = errors.RequestCancelledError(quiet=quiet) |
| 140 | + |
| 141 | + case httpx.RequestError(): |
| 142 | + req_uri = "<unknown-uri>" |
| 143 | + req_method = "<unknown-method>" |
| 144 | + if exception._request: |
| 145 | + req_uri = str(exception.request.url) |
| 146 | + req_method = exception.request.method |
| 147 | + |
| 148 | + formatted_exception = errors.BaseError( |
| 149 | + message=f"Error on remote connection {req_method} {req_uri}: {exception}" |
| 150 | + ) |
| 151 | + |
| 152 | + return formatted_exception |
| 153 | + |
| 154 | + def default(self, request: Request, exception: Exception) -> HTTPResponse: |
| 155 | + """Overrides the default error handler.""" |
| 156 | + formatted_exception = self._get_formatted_exception(request, exception) or errors.BaseError() |
| 157 | + |
139 | 158 | self.log(request, formatted_exception) |
140 | 159 | if formatted_exception.status_code == 500 and "PYTEST_CURRENT_TEST" in os.environ: |
141 | 160 | # TODO: Figure out how to do logging properly in here, I could not get the sanic logs to show up from here |
|
0 commit comments