18 lines
678 B
Python
18 lines
678 B
Python
from fastapi import Request
|
|
from fastapi.applications import AppType
|
|
from starlette.responses import JSONResponse
|
|
|
|
from utils.exceptions import ObjectNotFoundException, ForbiddenException
|
|
|
|
|
|
def register_exception_handlers(app: AppType):
|
|
@app.exception_handler(ObjectNotFoundException)
|
|
async def not_found_exception_handler(
|
|
request: Request, exc: ObjectNotFoundException
|
|
):
|
|
return JSONResponse(status_code=404, content={"detail": exc.name})
|
|
|
|
@app.exception_handler(ForbiddenException)
|
|
async def forbidden_exception_handler(request: Request, exc: ForbiddenException):
|
|
return JSONResponse(status_code=403, content={"detail": exc.name})
|