Skip to content

Commit

Permalink
⚡ replace FastAPI JSONResponse with ORJSONResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ff137 committed Oct 30, 2024
1 parent ee8912d commit 595f2bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from aries_cloudcontroller import ApiException
from fastapi import FastAPI, Request, Response
from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse
from fastapi.responses import ORJSONResponse
from scalar_fastapi import get_scalar_api_reference

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


@app.exception_handler(Exception)
async def universal_exception_handler(_: Request, exception: Exception) -> JSONResponse:
async def universal_exception_handler(
_: Request, exception: Exception
) -> ORJSONResponse:
stacktrace = {"traceback": traceback.format_exc()} if debug else {}

if isinstance(exception, CloudApiException):
return JSONResponse(
return ORJSONResponse(
content={"detail": exception.detail, **stacktrace},
status_code=exception.status_code,
)

if isinstance(exception, CloudApiValueError):
return JSONResponse(
return ORJSONResponse(
{"detail": exception.detail, **stacktrace},
status_code=422,
)

if isinstance(exception, pydantic.ValidationError):
return JSONResponse(
return ORJSONResponse(
{"detail": extract_validation_error_msg(exception), **stacktrace},
status_code=422,
)

if isinstance(exception, ApiException):
return JSONResponse(
return ORJSONResponse(
{"detail": exception.reason, **stacktrace},
status_code=exception.status,
)

if isinstance(exception, HTTPException):
return JSONResponse(
return ORJSONResponse(
{"detail": exception.detail, **stacktrace},
status_code=exception.status_code,
headers=exception.headers,
)

return JSONResponse(
return ORJSONResponse(
{"detail": "Internal server error", "exception": str(exception), **stacktrace},
status_code=500,
)
4 changes: 2 additions & 2 deletions app/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from aries_cloudcontroller import ApiException
from fastapi import HTTPException, Request
from fastapi.responses import JSONResponse
from fastapi.responses import ORJSONResponse

from app.exceptions.cloudapi_exception import CloudApiException
from app.main import (
Expand Down Expand Up @@ -109,6 +109,6 @@ async def test_universal_exception_handler():
for exception, expected_status, expected_detail in test_cases:
request = Mock(spec=Request)
response = await universal_exception_handler(request, exception)
assert isinstance(response, JSONResponse)
assert isinstance(response, ORJSONResponse)
assert response.status_code == expected_status
assert expected_detail in response.body.decode()

0 comments on commit 595f2bf

Please sign in to comment.