|
7 | 7 | from aries_cloudcontroller import ApiException
|
8 | 8 | from fastapi import FastAPI, Request, Response
|
9 | 9 | from fastapi.exceptions import HTTPException
|
10 |
| -from fastapi.responses import JSONResponse |
| 10 | +from fastapi.responses import ORJSONResponse |
11 | 11 | from scalar_fastapi import get_scalar_api_reference
|
12 | 12 |
|
13 | 13 | from app.exceptions import CloudApiException
|
@@ -134,41 +134,43 @@ def read_openapi_yaml() -> Response:
|
134 | 134 |
|
135 | 135 |
|
136 | 136 | @app.exception_handler(Exception)
|
137 |
| -async def universal_exception_handler(_: Request, exception: Exception) -> JSONResponse: |
| 137 | +async def universal_exception_handler( |
| 138 | + _: Request, exception: Exception |
| 139 | +) -> ORJSONResponse: |
138 | 140 | stacktrace = {"traceback": traceback.format_exc()} if debug else {}
|
139 | 141 |
|
140 | 142 | if isinstance(exception, CloudApiException):
|
141 |
| - return JSONResponse( |
| 143 | + return ORJSONResponse( |
142 | 144 | content={"detail": exception.detail, **stacktrace},
|
143 | 145 | status_code=exception.status_code,
|
144 | 146 | )
|
145 | 147 |
|
146 | 148 | if isinstance(exception, CloudApiValueError):
|
147 |
| - return JSONResponse( |
| 149 | + return ORJSONResponse( |
148 | 150 | {"detail": exception.detail, **stacktrace},
|
149 | 151 | status_code=422,
|
150 | 152 | )
|
151 | 153 |
|
152 | 154 | if isinstance(exception, pydantic.ValidationError):
|
153 |
| - return JSONResponse( |
| 155 | + return ORJSONResponse( |
154 | 156 | {"detail": extract_validation_error_msg(exception), **stacktrace},
|
155 | 157 | status_code=422,
|
156 | 158 | )
|
157 | 159 |
|
158 | 160 | if isinstance(exception, ApiException):
|
159 |
| - return JSONResponse( |
| 161 | + return ORJSONResponse( |
160 | 162 | {"detail": exception.reason, **stacktrace},
|
161 | 163 | status_code=exception.status,
|
162 | 164 | )
|
163 | 165 |
|
164 | 166 | if isinstance(exception, HTTPException):
|
165 |
| - return JSONResponse( |
| 167 | + return ORJSONResponse( |
166 | 168 | {"detail": exception.detail, **stacktrace},
|
167 | 169 | status_code=exception.status_code,
|
168 | 170 | headers=exception.headers,
|
169 | 171 | )
|
170 | 172 |
|
171 |
| - return JSONResponse( |
| 173 | + return ORJSONResponse( |
172 | 174 | {"detail": "Internal server error", "exception": str(exception), **stacktrace},
|
173 | 175 | status_code=500,
|
174 | 176 | )
|
0 commit comments