Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Rename error
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 12, 2024
1 parent a309f3e commit 7b3a36d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 9 additions & 1 deletion backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from starlette.requests import Request
from starlette.responses import JSONResponse

from db.errors.database_errors import ActionAlreadyPerformedError, ItemNotFoundError
from db.errors.database_errors import ActionAlreadyPerformedError, ItemNotFoundError, NoSuchRelationError
from routes.errors.authentication import InvalidRoleCredentialsError, NoAccessToSubjectError
from routes.group import group_router
from routes.project import project_router
Expand Down Expand Up @@ -74,5 +74,13 @@ def action_already_performed_error_handler(request: Request, exc: ActionAlreadyP
)


@app.exception_handler(NoSuchRelationError)
def no_such_relation_error_handler(request: Request, exc: NoSuchRelationError) -> JSONResponse:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": str(exc)},
)


if __name__ == "__main__":
uvicorn.run("app:app")
6 changes: 5 additions & 1 deletion backend/db/errors/database_errors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
class ItemNotFoundError(Exception):

def __init__(self, message: str) -> None:
super().__init__(message)


class ActionAlreadyPerformedError(Exception):
def __init__(self, message: str) -> None:
super().__init__(message)


class NoSuchRelationError(Exception):
def __init__(self, message: str) -> None:
super().__init__(message)
4 changes: 2 additions & 2 deletions backend/domain/logic/group.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlalchemy.orm import Session

from db.errors.database_errors import ActionAlreadyPerformedError
from db.errors.database_errors import ActionAlreadyPerformedError, NoSuchRelationError
from db.models.models import Group, Project, Student
from domain.logic.basic_operations import get, get_all
from domain.models.GroupDataclass import GroupDataclass
Expand Down Expand Up @@ -56,7 +56,7 @@ def remove_student_from_group(session: Session, student_id: int, group_id: int)

if student not in group.students:
msg = f"Student with id {student_id} is not in group with id {group_id}"
raise ActionAlreadyPerformedError(msg)
raise NoSuchRelationError(msg)

group.students.remove(student)
session.commit()
Expand Down

0 comments on commit 7b3a36d

Please sign in to comment.