Skip to content

Commit

Permalink
python: use custom handler for validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoto7250 committed Nov 28, 2024
1 parent 23e3954 commit dee45ca
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion webapp/python/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from http import HTTPStatus

from fastapi import FastAPI, HTTPException, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from sqlalchemy import text
Expand All @@ -28,12 +29,21 @@ class PostInitializeResponse(BaseModel):


@app.exception_handler(HTTPStatus.INTERNAL_SERVER_ERROR)
def internal_exception_handler(_request: Request, exc: Exception) -> JSONResponse:
def internal_exception_handler(_: Request, exc: Exception) -> JSONResponse:
return JSONResponse(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, content={"message": str(exc)}
)


@app.exception_handler(RequestValidationError)
def validation_exception_handler(
_: Request, exc: RequestValidationError
) -> JSONResponse:
return JSONResponse(
status_code=HTTPStatus.METHOD_NOT_ALLOWED, content={"message": str(exc)}
)


@app.exception_handler(HTTPException)
def custom_http_exception_handler(_: Request, exc: HTTPException) -> JSONResponse:
return JSONResponse(status_code=exc.status_code, content={"message": exc.detail})
Expand Down

0 comments on commit dee45ca

Please sign in to comment.