mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-19 03:54:13 +02:00
fixing code related to exception handling
This commit is contained in:
@@ -1,16 +1,57 @@
|
||||
class MediaAlreadyExists(ValueError):
|
||||
from fastapi import Request, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
class MediaAlreadyExists(Exception):
|
||||
"""Raised when a show already exists"""
|
||||
|
||||
def __init__(
|
||||
self, message: str = "Entity with this ID or other identifier already exists"
|
||||
):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class NotFoundError(Exception):
|
||||
"""Custom exception for when an entity is not found."""
|
||||
|
||||
def __init__(self, message: str = "The requested entity was not found."):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
pass
|
||||
|
||||
|
||||
class InvalidConfigError(Exception):
|
||||
"""Custom exception for when an entity is not found."""
|
||||
|
||||
def __init__(self, message: str = "The server is improperly configured."):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
pass
|
||||
|
||||
|
||||
async def media_already_exists_exception_handler(
|
||||
request: Request, exc: MediaAlreadyExists | Exception
|
||||
) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=401,
|
||||
content={"detail": exc.message},
|
||||
)
|
||||
|
||||
|
||||
async def not_found_error_exception_handler(
|
||||
request: Request, exc: NotFoundError | Exception
|
||||
) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content={"detail": exc.message},
|
||||
)
|
||||
|
||||
|
||||
async def invalid_config_error_exception_handler(
|
||||
request: Request, exc: InvalidConfigError | Exception
|
||||
) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"detail": exc.message},
|
||||
)
|
||||
Reference in New Issue
Block a user