-
Notifications
You must be signed in to change notification settings - Fork 0
/
exc_handlers.py
27 lines (20 loc) · 1006 Bytes
/
exc_handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import starlette
from starlette.responses import JSONResponse
from ..exceptions import AlreadyExists, Forbidden, ObjectNotFound
from ..schemas.response_model import StatusResponseModel
from .base import app
@app.exception_handler(ObjectNotFound)
async def not_found_handler(req: starlette.requests.Request, exc: ObjectNotFound):
return JSONResponse(
content=StatusResponseModel(status="Error", message=exc.en, ru=exc.ru).model_dump(), status_code=404
)
@app.exception_handler(Forbidden)
async def forbidden_handler(req: starlette.requests.Request, exc: Forbidden):
return JSONResponse(
content=StatusResponseModel(status="Forbidden", message=exc.en, ru=exc.ru).model_dump(), status_code=403
)
@app.exception_handler(AlreadyExists)
async def already_exists_handler(req: starlette.requests.Request, exc: AlreadyExists):
return JSONResponse(
content=StatusResponseModel(status="Already exists", message=exc.en, ru=exc.ru).model_dump(), status_code=409
)