From eba5aff056bfc3e246bd10cd31f1682a804adb9a Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Thu, 19 Oct 2023 16:20:40 +0100 Subject: [PATCH] Reuse MissingRecordError rather than using EntityNotFoundError --- inventory_management_system_api/core/breadcrumbs.py | 4 ++-- inventory_management_system_api/core/exceptions.py | 6 ------ .../routers/v1/catalogue_category.py | 3 +-- inventory_management_system_api/routers/v1/system.py | 3 +-- test/unit/core/test_breacrumbs.py | 4 ++-- 5 files changed, 6 insertions(+), 14 deletions(-) diff --git a/inventory_management_system_api/core/breadcrumbs.py b/inventory_management_system_api/core/breadcrumbs.py index 7d4f8086..524fc53c 100644 --- a/inventory_management_system_api/core/breadcrumbs.py +++ b/inventory_management_system_api/core/breadcrumbs.py @@ -7,7 +7,7 @@ from inventory_management_system_api.core.consts import BREADCRUMBS_TRAIL_MAX_LENGTH from inventory_management_system_api.core.custom_object_id import CustomObjectId -from inventory_management_system_api.core.exceptions import DatabaseIntegrityError, EntityNotFoundError +from inventory_management_system_api.core.exceptions import DatabaseIntegrityError, MissingRecordError from inventory_management_system_api.schemas.breadcrumbs import BreadcrumbsGetSchema logger = logging.getLogger() @@ -67,7 +67,7 @@ def query_breadcrumbs(entity_id: str, entity_collection: Collection, graph_looku ) )[0]["result"] if len(result) == 0: - raise EntityNotFoundError(f"Entity with the ID {entity_id} was not found in the collection {graph_lookup_from}") + raise MissingRecordError(f"Entity with the ID {entity_id} was not found in the collection {graph_lookup_from}") for element in result: trail.append((str(element["_id"]), element["name"])) full_trail = result[0]["parent_id"] is None diff --git a/inventory_management_system_api/core/exceptions.py b/inventory_management_system_api/core/exceptions.py index 4e6d582a..52fc91ca 100644 --- a/inventory_management_system_api/core/exceptions.py +++ b/inventory_management_system_api/core/exceptions.py @@ -61,9 +61,3 @@ class DatabaseIntegrityError(DatabaseError): """ Exception raised when something is found in the database that shouldn't have been """ - - -class EntityNotFoundError(Exception): - """ - An entity with a given ID was not found - """ diff --git a/inventory_management_system_api/routers/v1/catalogue_category.py b/inventory_management_system_api/routers/v1/catalogue_category.py index 79a85def..a4ca7216 100644 --- a/inventory_management_system_api/routers/v1/catalogue_category.py +++ b/inventory_management_system_api/routers/v1/catalogue_category.py @@ -11,7 +11,6 @@ ChildrenElementsExistError, DatabaseIntegrityError, DuplicateRecordError, - EntityNotFoundError, InvalidObjectIdError, LeafCategoryError, MissingRecordError, @@ -76,7 +75,7 @@ def get_catalogue_category_breadcrumbs( # pylint: disable=missing-function-docstring try: return catalogue_category_service.get_breadcrumbs(catalogue_category_id) - except (InvalidObjectIdError, EntityNotFoundError) as exc: + except (MissingRecordError, InvalidObjectIdError) as exc: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Catalogue category with such ID was not found" ) from exc diff --git a/inventory_management_system_api/routers/v1/system.py b/inventory_management_system_api/routers/v1/system.py index 9353b155..4e813c77 100644 --- a/inventory_management_system_api/routers/v1/system.py +++ b/inventory_management_system_api/routers/v1/system.py @@ -12,7 +12,6 @@ ChildrenElementsExistError, DatabaseIntegrityError, DuplicateRecordError, - EntityNotFoundError, InvalidObjectIdError, MissingRecordError, ) @@ -69,7 +68,7 @@ def get_system_breadcrumbs( # pylint: disable=duplicate-code try: return system_service.get_breadcrumbs(system_id) - except (InvalidObjectIdError, EntityNotFoundError) as exc: + except (MissingRecordError, InvalidObjectIdError) as exc: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="System with such ID was not found") from exc except DatabaseIntegrityError as exc: raise HTTPException( diff --git a/test/unit/core/test_breacrumbs.py b/test/unit/core/test_breacrumbs.py index 1dfb1e2f..1b36532f 100644 --- a/test/unit/core/test_breacrumbs.py +++ b/test/unit/core/test_breacrumbs.py @@ -12,8 +12,8 @@ from inventory_management_system_api.core.custom_object_id import CustomObjectId from inventory_management_system_api.core.exceptions import ( DatabaseIntegrityError, - EntityNotFoundError, InvalidObjectIdError, + MissingRecordError, ) MOCK_AGGREGATE_RESPONSE_LESS_THAN_MAX_LENGTH = [ @@ -138,7 +138,7 @@ def test_query_breadcrumbs_when_entity_not_found(self): mock_entity_collection.aggregate.return_value = MOCK_AGGREGATE_RESPONSE_NON_EXISTENT_ID - with pytest.raises(EntityNotFoundError) as exc: + with pytest.raises(MissingRecordError) as exc: breadcrumbs.query_breadcrumbs( entity_id=entity_id, entity_collection=mock_entity_collection, graph_lookup_from=graph_lookup_from )