Implement exception handler
This commit is contained in:
1
core/scripts/webpanel/exception_handler/__init__.py
Normal file
1
core/scripts/webpanel/exception_handler/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from .handler import setup_exception_handler
|
||||||
20
core/scripts/webpanel/exception_handler/handler.py
Normal file
20
core/scripts/webpanel/exception_handler/handler.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
from fastapi import FastAPI, HTTPException, Request
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class JSONErrorResponse(BaseModel):
|
||||||
|
status: int
|
||||||
|
detail: str
|
||||||
|
|
||||||
|
|
||||||
|
def setup_exception_handler(app: FastAPI):
|
||||||
|
'''
|
||||||
|
Setup exception handler for FastAPI.
|
||||||
|
'''
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def http_exception_handler(request: Request, exc: HTTPException): # type: ignore
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=exc.status_code,
|
||||||
|
content=JSONErrorResponse(status=exc.status_code, detail=exc.detail).model_dump(),
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user