Initial commit
This commit is contained in:
73
extensions/exceptions.py
Normal file
73
extensions/exceptions.py
Normal file
@@ -0,0 +1,73 @@
|
||||
from fastapi import Request
|
||||
from fastapi.responses import UJSONResponse
|
||||
from starlette.status import (
|
||||
HTTP_400_BAD_REQUEST,
|
||||
HTTP_401_UNAUTHORIZED,
|
||||
HTTP_404_NOT_FOUND,
|
||||
HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
)
|
||||
|
||||
from classes.exceptions import (
|
||||
EntrySearchQueryEmptyError,
|
||||
LocationNotFoundError,
|
||||
LocationSearchQueryEmptyError,
|
||||
SearchLimitInvalidError,
|
||||
SearchPageInvalidError,
|
||||
)
|
||||
from modules.app import app
|
||||
|
||||
|
||||
@app.exception_handler(LocationNotFoundError)
|
||||
async def location_not_found_exception_handler(
|
||||
request: Request, exc: LocationNotFoundError
|
||||
):
|
||||
return UJSONResponse(
|
||||
status_code=HTTP_404_NOT_FOUND,
|
||||
content={"detail": f"Could not find location with id '{exc.id}'."},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(EntrySearchQueryEmptyError)
|
||||
async def entry_search_query_empty_exception_handler(
|
||||
request: Request, exc: EntrySearchQueryEmptyError
|
||||
):
|
||||
return UJSONResponse(
|
||||
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
content={
|
||||
"detail": "You must provide location and date(s) to look for entries."
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(LocationSearchQueryEmptyError)
|
||||
async def location_search_query_empty_exception_handler(
|
||||
request: Request, exc: LocationSearchQueryEmptyError
|
||||
):
|
||||
return UJSONResponse(
|
||||
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
content={
|
||||
"detail": "You must provide name or coordinates to look for location."
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(SearchLimitInvalidError)
|
||||
async def search_limit_invalid_exception_handler(
|
||||
request: Request, exc: SearchLimitInvalidError
|
||||
):
|
||||
return UJSONResponse(
|
||||
status_code=HTTP_401_UNAUTHORIZED,
|
||||
content={"detail": "Parameter 'limit' must be greater or equal to 1."},
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(SearchPageInvalidError)
|
||||
async def search_page_invalid_exception_handler(
|
||||
request: Request, exc: SearchPageInvalidError
|
||||
):
|
||||
return UJSONResponse(
|
||||
status_code=HTTP_400_BAD_REQUEST,
|
||||
content={
|
||||
"detail": "Parameters 'page' and 'page_size' must be greater or equal to 1."
|
||||
},
|
||||
)
|
Reference in New Issue
Block a user