mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-02-20 07:59:50 -05:00
add proper http error codes
This commit is contained in:
@@ -6,6 +6,8 @@ from media_manager.database import DbSessionDependency
|
||||
from media_manager.tv.repository import TvRepository
|
||||
from media_manager.tv.schemas import Show, ShowId, SeasonId, Season
|
||||
from media_manager.tv.service import TvService
|
||||
from media_manager.tv.exceptions import NotFoundError
|
||||
from fastapi import HTTPException
|
||||
|
||||
|
||||
def get_tv_repository(db_session: DbSessionDependency) -> TvRepository:
|
||||
@@ -28,7 +30,13 @@ def get_show_by_id(
|
||||
tv_service: tv_service_dep,
|
||||
show_id: ShowId = Path(..., description="The ID of the show"),
|
||||
) -> Show:
|
||||
show = tv_service.get_show_by_id(show_id)
|
||||
try:
|
||||
show = tv_service.get_show_by_id(show_id)
|
||||
except NotFoundError:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Show with ID {show_id} not found.",
|
||||
)
|
||||
return show
|
||||
|
||||
|
||||
@@ -39,7 +47,14 @@ def get_season_by_id(
|
||||
tv_service: tv_service_dep,
|
||||
season_id: SeasonId = Path(..., description="The ID of the season"),
|
||||
) -> Season:
|
||||
return tv_service.get_season(season_id=season_id)
|
||||
try:
|
||||
season = tv_service.get_season(season_id=season_id)
|
||||
except NotFoundError:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Season with ID {season_id} not found.",
|
||||
)
|
||||
return season
|
||||
|
||||
|
||||
season_dep = Annotated[Season, Depends(get_season_by_id)]
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
class MediaAlreadyExists(ValueError):
|
||||
"""Raised when a show already exists"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class NotFoundError(Exception):
|
||||
"""Custom exception for when an entity is not found."""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from fastapi import APIRouter, Depends, status, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from media_manager.auth.db import User
|
||||
@@ -67,7 +67,7 @@ def add_a_show(
|
||||
|
||||
@router.delete(
|
||||
"/shows/{show_id}",
|
||||
status_code=status.HTTP_200_OK,
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
dependencies=[Depends(current_active_user)],
|
||||
)
|
||||
def delete_a_show(tv_repository: tv_repository_dep, show_id: ShowId):
|
||||
@@ -171,13 +171,14 @@ def delete_season_request(
|
||||
if user.is_superuser or request.requested_by.id == user.id:
|
||||
tv_service.delete_season_request(season_request_id=request_id)
|
||||
log.info(f"User {user.id} deleted season request {request_id}.")
|
||||
return None
|
||||
else:
|
||||
log.warning(
|
||||
f"User {user.id} tried to delete season request {request_id} but is not authorized."
|
||||
)
|
||||
return JSONResponse(
|
||||
return HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
content={"message": "Not authorized to delete this request."},
|
||||
detail="Not authorized to delete this request",
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user