From 7b3a36df8ccbf2ef4236d30db056cada51203d37 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Tue, 12 Mar 2024 17:25:51 +0100 Subject: [PATCH] Rename error --- backend/app.py | 10 +++++++++- backend/db/errors/database_errors.py | 6 +++++- backend/domain/logic/group.py | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/backend/app.py b/backend/app.py index 030c32e4..bbe337c3 100644 --- a/backend/app.py +++ b/backend/app.py @@ -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 @@ -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") diff --git a/backend/db/errors/database_errors.py b/backend/db/errors/database_errors.py index e2805cc3..05a400e7 100644 --- a/backend/db/errors/database_errors.py +++ b/backend/db/errors/database_errors.py @@ -1,5 +1,4 @@ class ItemNotFoundError(Exception): - def __init__(self, message: str) -> None: super().__init__(message) @@ -7,3 +6,8 @@ def __init__(self, message: str) -> None: class ActionAlreadyPerformedError(Exception): def __init__(self, message: str) -> None: super().__init__(message) + + +class NoSuchRelationError(Exception): + def __init__(self, message: str) -> None: + super().__init__(message) diff --git a/backend/domain/logic/group.py b/backend/domain/logic/group.py index 788e95d8..eaab0039 100644 --- a/backend/domain/logic/group.py +++ b/backend/domain/logic/group.py @@ -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 @@ -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()