From b5eb8b7ee8c1c995faedc05b688edd6ce10d0b84 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 14 Dec 2022 14:33:13 +0100 Subject: [PATCH] add handler for RequestValidationError --- lnbits/app.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lnbits/app.py b/lnbits/app.py index c293a445..b688ec8d 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -184,10 +184,32 @@ def register_exception_handlers(app: FastAPI): ) return JSONResponse( - status_code=HTTPStatus.BAD_REQUEST, + status_code=HTTPStatus.NO_CONTENT, content={"detail": exc_str}, ) + @app.exception_handler(RequestValidationError) + async def validation_exception_handler( + request: Request, exc: RequestValidationError + ): + # Only the browser sends "text/html" request + # not fail proof, but everything else get's a JSON response + + if ( + request.headers + and "accept" in request.headers + and "text/html" in request.headers["accept"] + ): + return template_renderer().TemplateResponse( + "error.html", + {"request": request, "err": f"{exc.errors()} is not a valid UUID."}, + ) + + return JSONResponse( + status_code=HTTPStatus.NO_CONTENT, + content={"detail": exc.errors()}, + ) + def configure_logger() -> None: logger.remove()