Skip to content

Commit

Permalink
Nicer user errors (first step)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishefi committed Apr 17, 2024
1 parent a8f3f18 commit 3996022
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from fastapi.staticfiles import StaticFiles

from common import config
from common.error import HSError
from common.session import get_model
from common.session import get_mongo
from common.session import get_redis
Expand Down Expand Up @@ -130,6 +131,16 @@ async def get_user(
return await call_next(request)


@app.middleware("http")
async def catch_known_errors(
request: Request, call_next: Callable[[Request], Awaitable[Response]]
) -> Response:
try:
return await call_next(request)
except HSError as e:
return JSONResponse(content=str(e), status_code=status.HTTP_400_BAD_REQUEST)


@app.get("/health")
async def health() -> dict[str, str]:
return {"message": "Healthy!"}
Expand Down

0 comments on commit 3996022

Please sign in to comment.