2023-02-16 16:04:28 +02:00
|
|
|
from fastapi import Request
|
|
|
|
from fastapi.responses import UJSONResponse
|
2023-02-16 15:11:29 +02:00
|
|
|
from modules.app import app
|
|
|
|
from classes.exceptions import *
|
2023-02-16 16:44:54 +02:00
|
|
|
from starlette.status import HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND, HTTP_406_NOT_ACCEPTABLE, HTTP_409_CONFLICT, HTTP_422_UNPROCESSABLE_ENTITY
|
2023-02-16 15:11:29 +02:00
|
|
|
|
|
|
|
@app.exception_handler(AlbumNotFoundError)
|
|
|
|
async def album_not_found_exception_handler(request: Request, exc: AlbumNotFoundError):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:11:29 +02:00
|
|
|
status_code=HTTP_404_NOT_FOUND,
|
2023-02-16 15:55:03 +02:00
|
|
|
content={"detail": f"Could not find album with id '{exc.id}'."},
|
2023-02-16 15:11:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(AlbumAlreadyExistsError)
|
|
|
|
async def album_already_exists_exception_handler(request: Request, exc: AlbumAlreadyExistsError):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:11:29 +02:00
|
|
|
status_code=HTTP_409_CONFLICT,
|
|
|
|
content={"detail": f"Album with name '{exc.name}' already exists."},
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(AlbumIncorrectError)
|
|
|
|
async def album_incorrect_exception_handler(request: Request, exc: AlbumIncorrectError):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:11:29 +02:00
|
|
|
status_code=HTTP_406_NOT_ACCEPTABLE,
|
|
|
|
content={"detail": f"Album {exc.place} invalid: {exc.error}"},
|
2023-02-16 15:55:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(PhotoNotFoundError)
|
|
|
|
async def photo_not_found_exception_handler(request: Request, exc: PhotoNotFoundError):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:55:03 +02:00
|
|
|
status_code=HTTP_404_NOT_FOUND,
|
|
|
|
content={"detail": f"Could not find photo with id '{exc.id}'."},
|
|
|
|
)
|
|
|
|
|
2023-02-16 16:44:54 +02:00
|
|
|
@app.exception_handler(PhotoSearchQueryEmptyError)
|
|
|
|
async def photo_search_query_empty_exception_handler(request: Request, exc: PhotoSearchQueryEmptyError):
|
|
|
|
return UJSONResponse(
|
|
|
|
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
content={"detail": "You must provide query, caption or coordinates to look for photos."},
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(VideoNotFoundError)
|
|
|
|
async def video_not_found_exception_handler(request: Request, exc: VideoNotFoundError):
|
|
|
|
return UJSONResponse(
|
|
|
|
status_code=HTTP_404_NOT_FOUND,
|
|
|
|
content={"detail": f"Could not find video with id '{exc.id}'."},
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(VideoSearchQueryEmptyError)
|
|
|
|
async def video_search_query_empty_exception_handler(request: Request, exc: VideoSearchQueryEmptyError):
|
|
|
|
return UJSONResponse(
|
|
|
|
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
content={"detail": "You must provide query, caption or coordinates to look for photos."},
|
|
|
|
)
|
|
|
|
|
2023-02-16 15:55:03 +02:00
|
|
|
@app.exception_handler(SearchPageInvalidError)
|
|
|
|
async def search_page_invalid_exception_handler(request: Request, exc: SearchPageInvalidError):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:55:03 +02:00
|
|
|
status_code=HTTP_400_BAD_REQUEST,
|
|
|
|
content={"detail": "Parameters 'page' and 'page_size' must be greater or equal to 1."},
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(SearchTokenInvalidError)
|
|
|
|
async def search_token_invalid_exception_handler(request: Request, exc: SearchTokenInvalidError):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:55:03 +02:00
|
|
|
status_code=HTTP_401_UNAUTHORIZED,
|
|
|
|
content={"detail": "Parameters 'page' and 'page_size' must be greater or equal to 1."},
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(UserEmailCodeInvalid)
|
|
|
|
async def user_email_code_invalid_exception_handler(request: Request, exc: UserEmailCodeInvalid):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:55:03 +02:00
|
|
|
status_code=HTTP_400_BAD_REQUEST,
|
|
|
|
content={"detail": "Confirmation code is invalid."},
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(UserAlreadyExists)
|
|
|
|
async def user_already_exists_exception_handler(request: Request, exc: UserAlreadyExists):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:55:03 +02:00
|
|
|
status_code=HTTP_409_CONFLICT,
|
|
|
|
content={"detail": "User with this username already exists."},
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.exception_handler(UserCredentialsInvalid)
|
|
|
|
async def user_credentials_invalid_exception_handler(request: Request, exc: UserCredentialsInvalid):
|
2023-02-16 16:04:28 +02:00
|
|
|
return UJSONResponse(
|
2023-02-16 15:55:03 +02:00
|
|
|
status_code=HTTP_401_UNAUTHORIZED,
|
|
|
|
content={"detail": "Invalid credentials."},
|
2023-02-16 15:11:29 +02:00
|
|
|
)
|