From 47f171347c04fcc9594926b0a7e0f22f6715c3b4 Mon Sep 17 00:00:00 2001 From: yatao Date: Wed, 25 Dec 2024 23:47:07 +0800 Subject: [PATCH] add APIResponseExceptionHandler --- appboot/asgi.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/appboot/asgi.py b/appboot/asgi.py index 4c5bd3f..f80b104 100644 --- a/appboot/asgi.py +++ b/appboot/asgi.py @@ -7,6 +7,7 @@ from appboot.conf import settings from appboot.db import transaction from appboot.exceptions import Error +from appboot.response import APIResponse class ExceptionHandler: @@ -27,6 +28,12 @@ async def handle_exception(cls, request: Request, exc: Exception): return await cls(request, exc).make_response() +class APIResponseExceptionHandler(ExceptionHandler): + async def make_response(self): + data = APIResponse.from_exception(self.exc) + return JSONResponse(data.dict(exclude_unset=True), status_code=data.code) + + def get_asgi_application(): return get_fastapi_application()