1
0
mirror of https://github.com/etesync/server synced 2024-12-26 17:28:08 +00:00

Fix issue with how we render binary 422 errors

json_encoder assumes it's not binary, but our error messages may include
some binary stuff.
This commit is contained in:
Tom Hacohen 2024-06-08 22:53:56 -04:00
parent 043dc972ae
commit f1c072bd0a

View File

@ -1,8 +1,9 @@
import typing as t
from django.conf import settings
# Not at the top of the file because we first need to setup django
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
@ -77,9 +78,12 @@ def create_application(prefix="", middlewares=[]):
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
from pydantic import TypeAdapter
errors = TypeAdapter(t.Dict[str, t.Any])
return MsgpackResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": exc.errors()}),
content=errors.dump_python({"detail": exc.errors()}),
)
app.mount(settings.STATIC_URL, StaticFiles(directory=settings.STATIC_ROOT), name="static")