aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2024-07-11 22:29:58 +0100
committerGravatar Joe Banks <[email protected]>2024-07-11 22:29:58 +0100
commitcab8799fb83708e87cfcb602064372aa06d117d3 (patch)
treec81eb37cd8bed806b0575796cd05dc488c346d82
parentMerge pull request #281 from python-discord/jb3/components/vote-field (diff)
Return errors in JSON format so they can still be easily parsed
-rw-r--r--backend/__init__.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/backend/__init__.py b/backend/__init__.py
index 67015d7..c2e1335 100644
--- a/backend/__init__.py
+++ b/backend/__init__.py
@@ -1,9 +1,12 @@
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from starlette.applications import Starlette
+from starlette.exceptions import HTTPException
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.cors import CORSMiddleware
+from starlette.requests import Request
+from starlette.responses import JSONResponse
from backend import constants
from backend.authentication import JWTAuthenticationBackend
@@ -47,5 +50,14 @@ middleware = [
Middleware(ProtectedDocsMiddleware),
]
-app = Starlette(routes=create_route_map(), middleware=middleware)
+
+async def http_exception(_request: Request, exc: HTTPException) -> JSONResponse: # noqa: RUF029
+ return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)
+
+
+exception_handlers = {HTTPException: http_exception}
+
+app = Starlette(
+ routes=create_route_map(), middleware=middleware, exception_handlers=exception_handlers
+)
api.register(app)