From a27ce2f4d0ece74392b5632a7da400a79ecd903e Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sat, 8 Jun 2024 20:41:01 -0400 Subject: [PATCH] Also handle 422 as msgpack. --- etebase_server/fastapi/main.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/etebase_server/fastapi/main.py b/etebase_server/fastapi/main.py index 9e80dba..5d87a84 100644 --- a/etebase_server/fastapi/main.py +++ b/etebase_server/fastapi/main.py @@ -1,7 +1,9 @@ from django.conf import settings # Not at the top of the file because we first need to setup django -from fastapi import FastAPI, Request +from fastapi import FastAPI, Request, status +from fastapi.encoders import jsonable_encoder +from fastapi.exceptions import RequestValidationError from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.trustedhost import TrustedHostMiddleware from fastapi.staticfiles import StaticFiles @@ -73,6 +75,13 @@ def create_application(prefix="", middlewares=[]): async def custom_exception_handler(request: Request, exc: CustomHttpException): return MsgpackResponse(status_code=exc.status_code, content=exc.as_dict) + @app.exception_handler(RequestValidationError) + async def validation_exception_handler(request: Request, exc: RequestValidationError): + return MsgpackResponse( + status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, + content=jsonable_encoder({"detail": exc.errors()}), + ) + app.mount(settings.STATIC_URL, StaticFiles(directory=settings.STATIC_ROOT), name="static") return app