PhotosAPI/extensions/exceptions.py

183 lines
5.2 KiB
Python
Raw Normal View History

2023-03-12 15:59:13 +02:00
from fastapi import Request
2023-02-16 16:04:28 +02:00
from fastapi.responses import UJSONResponse
2023-03-12 15:59:13 +02:00
from starlette.status import (
HTTP_400_BAD_REQUEST,
HTTP_401_UNAUTHORIZED,
2023-11-25 18:50:09 +02:00
HTTP_403_FORBIDDEN,
2023-03-12 15:59:13 +02:00
HTTP_404_NOT_FOUND,
HTTP_406_NOT_ACCEPTABLE,
HTTP_409_CONFLICT,
HTTP_422_UNPROCESSABLE_ENTITY,
)
2023-06-22 14:26:01 +03:00
from classes.exceptions import (
2023-11-25 18:50:09 +02:00
AccessTokenInvalidError,
2023-06-22 14:26:01 +03:00
AlbumAlreadyExistsError,
AlbumIncorrectError,
2023-11-25 18:50:09 +02:00
AlbumNotFoundError,
2023-06-22 14:26:01 +03:00
PhotoNotFoundError,
PhotoSearchQueryEmptyError,
SearchLimitInvalidError,
2023-06-22 14:26:01 +03:00
SearchPageInvalidError,
SearchTokenInvalidError,
UserAlreadyExists,
UserCredentialsInvalid,
2023-11-25 18:50:09 +02:00
UserEmailCodeInvalid,
UserMediaQuotaReached,
VideoNotFoundError,
VideoSearchQueryEmptyError,
2023-06-22 14:26:01 +03:00
)
2023-06-22 14:17:53 +03:00
from modules.app import app
@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(
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-03-12 15:59:13 +02:00
@app.exception_handler(AlbumAlreadyExistsError)
2023-03-12 15:59:13 +02:00
async def album_already_exists_exception_handler(
request: Request, exc: AlbumAlreadyExistsError
):
2023-02-16 16:04:28 +02:00
return UJSONResponse(
status_code=HTTP_409_CONFLICT,
content={"detail": f"Album with name '{exc.name}' already exists."},
)
2023-03-12 15:59:13 +02:00
@app.exception_handler(AlbumIncorrectError)
async def album_incorrect_exception_handler(request: Request, exc: AlbumIncorrectError):
2023-02-16 16:04:28 +02:00
return UJSONResponse(
status_code=HTTP_406_NOT_ACCEPTABLE,
content={"detail": f"Album {exc.place} invalid: {exc.error}"},
2023-02-16 15:55:03 +02:00
)
2023-03-12 15:59:13 +02:00
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-03-12 15:59:13 +02:00
2023-02-16 16:44:54 +02:00
@app.exception_handler(PhotoSearchQueryEmptyError)
2023-03-12 15:59:13 +02:00
async def photo_search_query_empty_exception_handler(
request: Request, exc: PhotoSearchQueryEmptyError
):
2023-02-16 16:44:54 +02:00
return UJSONResponse(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
2023-03-12 15:59:13 +02:00
content={
"detail": "You must provide query, caption or coordinates to look for photos."
},
2023-02-16 16:44:54 +02:00
)
2023-03-12 15:59:13 +02:00
2023-02-16 16:44:54 +02:00
@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}'."},
)
2023-03-12 15:59:13 +02:00
2023-02-16 16:44:54 +02:00
@app.exception_handler(VideoSearchQueryEmptyError)
2023-03-12 15:59:13 +02:00
async def video_search_query_empty_exception_handler(
request: Request, exc: VideoSearchQueryEmptyError
):
2023-02-16 16:44:54 +02:00
return UJSONResponse(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
2023-03-12 15:59:13 +02:00
content={
"detail": "You must provide query, caption or coordinates to look for photos."
},
2023-02-16 16:44:54 +02:00
)
2023-03-12 15:59:13 +02:00
2023-12-14 01:20:42 +02:00
@app.exception_handler(SearchLimitInvalidError)
async def search_limit_invalid_exception_handler(
request: Request, exc: SearchLimitInvalidError
2023-03-12 15:59:13 +02:00
):
2023-02-16 16:04:28 +02:00
return UJSONResponse(
2023-02-16 15:55:03 +02:00
status_code=HTTP_400_BAD_REQUEST,
2023-03-12 15:59:13 +02:00
content={
2023-12-14 01:20:42 +02:00
"detail": "Parameter 'limit' must be greater or equal to 1."
2023-03-12 15:59:13 +02:00
},
2023-02-16 15:55:03 +02:00
)
2023-03-12 15:59:13 +02:00
@app.exception_handler(SearchPageInvalidError)
2023-12-14 01:20:42 +02:00
async def search_page_invalid_exception_handler(
request: Request, exc: SearchPageInvalidError
):
return UJSONResponse(
status_code=HTTP_401_UNAUTHORIZED,
content={
"detail": "Parameters 'page' and 'page_size' must be greater or equal to 1."
},
)
2023-02-16 15:55:03 +02:00
@app.exception_handler(SearchTokenInvalidError)
2023-03-12 15:59:13 +02:00
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,
2023-03-12 15:59:13 +02:00
content={
"detail": "Invalid search token."
2023-03-12 15:59:13 +02:00
},
2023-02-16 15:55:03 +02:00
)
2023-03-12 15:59:13 +02:00
2023-02-16 15:55:03 +02:00
@app.exception_handler(UserEmailCodeInvalid)
2023-03-12 15:59:13 +02:00
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."},
)
2023-03-12 15:59:13 +02:00
2023-02-16 15:55:03 +02:00
@app.exception_handler(UserAlreadyExists)
2023-03-12 15:59:13 +02:00
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."},
)
2023-03-12 15:59:13 +02:00
2023-02-18 01:19:46 +02:00
@app.exception_handler(AccessTokenInvalidError)
2023-03-12 15:59:13 +02:00
async def access_token_invalid_exception_handler(
request: Request, exc: AccessTokenInvalidError
):
2023-02-18 01:19:46 +02:00
return UJSONResponse(
status_code=HTTP_401_UNAUTHORIZED,
content={"detail": "Invalid access token."},
)
2023-03-12 15:59:13 +02:00
2023-02-16 15:55:03 +02:00
@app.exception_handler(UserCredentialsInvalid)
2023-03-12 15:59:13 +02:00
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-03-12 15:59:13 +02:00
)
2023-11-25 18:50:09 +02:00
@app.exception_handler(UserMediaQuotaReached)
async def user_media_quota_reached_exception_handler(
request: Request, exc: UserMediaQuotaReached
):
return UJSONResponse(
status_code=HTTP_403_FORBIDDEN,
content={"detail": "Media quota has been reached, media upload impossible."},
)