Skip to content

Commit cab8799

Browse files
committed
Return errors in JSON format so they can still be easily parsed
1 parent cdda92c commit cab8799

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

backend/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import sentry_sdk
22
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
33
from starlette.applications import Starlette
4+
from starlette.exceptions import HTTPException
45
from starlette.middleware import Middleware
56
from starlette.middleware.authentication import AuthenticationMiddleware
67
from starlette.middleware.cors import CORSMiddleware
8+
from starlette.requests import Request
9+
from starlette.responses import JSONResponse
710

811
from backend import constants
912
from backend.authentication import JWTAuthenticationBackend
@@ -47,5 +50,14 @@
4750
Middleware(ProtectedDocsMiddleware),
4851
]
4952

50-
app = Starlette(routes=create_route_map(), middleware=middleware)
53+
54+
async def http_exception(_request: Request, exc: HTTPException) -> JSONResponse: # noqa: RUF029
55+
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)
56+
57+
58+
exception_handlers = {HTTPException: http_exception}
59+
60+
app = Starlette(
61+
routes=create_route_map(), middleware=middleware, exception_handlers=exception_handlers
62+
)
5163
api.register(app)

0 commit comments

Comments
 (0)