Skip to content

Commit 5d2e29c

Browse files
authored
⚡ replace FastAPI JSONResponse with ORJSONResponse (#1144)
1 parent ee8912d commit 5d2e29c

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

app/main.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from aries_cloudcontroller import ApiException
88
from fastapi import FastAPI, Request, Response
99
from fastapi.exceptions import HTTPException
10-
from fastapi.responses import JSONResponse
10+
from fastapi.responses import ORJSONResponse
1111
from scalar_fastapi import get_scalar_api_reference
1212

1313
from app.exceptions import CloudApiException
@@ -134,41 +134,43 @@ def read_openapi_yaml() -> Response:
134134

135135

136136
@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:
138140
stacktrace = {"traceback": traceback.format_exc()} if debug else {}
139141

140142
if isinstance(exception, CloudApiException):
141-
return JSONResponse(
143+
return ORJSONResponse(
142144
content={"detail": exception.detail, **stacktrace},
143145
status_code=exception.status_code,
144146
)
145147

146148
if isinstance(exception, CloudApiValueError):
147-
return JSONResponse(
149+
return ORJSONResponse(
148150
{"detail": exception.detail, **stacktrace},
149151
status_code=422,
150152
)
151153

152154
if isinstance(exception, pydantic.ValidationError):
153-
return JSONResponse(
155+
return ORJSONResponse(
154156
{"detail": extract_validation_error_msg(exception), **stacktrace},
155157
status_code=422,
156158
)
157159

158160
if isinstance(exception, ApiException):
159-
return JSONResponse(
161+
return ORJSONResponse(
160162
{"detail": exception.reason, **stacktrace},
161163
status_code=exception.status,
162164
)
163165

164166
if isinstance(exception, HTTPException):
165-
return JSONResponse(
167+
return ORJSONResponse(
166168
{"detail": exception.detail, **stacktrace},
167169
status_code=exception.status_code,
168170
headers=exception.headers,
169171
)
170172

171-
return JSONResponse(
173+
return ORJSONResponse(
172174
{"detail": "Internal server error", "exception": str(exception), **stacktrace},
173175
status_code=500,
174176
)

app/tests/test_main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from aries_cloudcontroller import ApiException
66
from fastapi import HTTPException, Request
7-
from fastapi.responses import JSONResponse
7+
from fastapi.responses import ORJSONResponse
88

99
from app.exceptions.cloudapi_exception import CloudApiException
1010
from app.main import (
@@ -109,6 +109,6 @@ async def test_universal_exception_handler():
109109
for exception, expected_status, expected_detail in test_cases:
110110
request = Mock(spec=Request)
111111
response = await universal_exception_handler(request, exception)
112-
assert isinstance(response, JSONResponse)
112+
assert isinstance(response, ORJSONResponse)
113113
assert response.status_code == expected_status
114114
assert expected_detail in response.body.decode()

0 commit comments

Comments
 (0)