Skip to content

Commit c82a0dc

Browse files
committed
Simplify missing record errors #74
1 parent f776aa3 commit c82a0dc

File tree

14 files changed

+45
-84
lines changed

14 files changed

+45
-84
lines changed

inventory_management_system_api/core/exceptions.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,19 @@ class MissingRecordError(DatabaseError):
114114
A specific database record was requested but could not be found.
115115
"""
116116

117-
status_code = status.HTTP_404_NOT_FOUND
118-
response_detail = "Requested record was not found"
117+
def __init__(self, entity_id: str, entity_type: str, use_422=False):
118+
"""
119+
Initialise the exception.
120+
121+
:param entity_id: ID of the record that was found to be missing.
122+
:param entity_type: Name of the entity type e.g. catalogue categories/systems (Used for logging).
123+
:param use_422: Whether the error returned if uncaught should be a 422 (default is 404 when false).
124+
"""
125+
super().__init__(
126+
detail=f"No {entity_type} found with ID: {entity_id}",
127+
response_detail=f"{entity_type.capitalize()} not found",
128+
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if use_422 else status.HTTP_404_NOT_FOUND,
129+
)
119130

120131

121132
class ChildElementsExistError(DatabaseError):

inventory_management_system_api/repositories/catalogue_category.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from datetime import datetime, timezone
77
from typing import List, Optional
88

9-
from fastapi import status
109
from pymongo.client_session import ClientSession
1110
from pymongo.collection import Collection
1211

@@ -69,7 +68,8 @@ def create(
6968
if self._is_duplicate_catalogue_category(parent_id, catalogue_category.code, session=session):
7069
raise DuplicateRecordError(
7170
"Duplicate catalogue category found within the parent catalogue category",
72-
"A catalogue category with the same name already exists within the parent catalogue category",
71+
response_detail="A catalogue category with the same name already exists within the parent catalogue "
72+
"category",
7373
)
7474

7575
logger.info("Inserting the new catalogue category into the database")
@@ -107,9 +107,7 @@ def get(
107107
if catalogue_category:
108108
return CatalogueCategoryOut(**catalogue_category)
109109
raise MissingRecordError(
110-
detail=f"No {entity_type} found with ID: {catalogue_category_id}",
111-
response_detail=f"{entity_type.capitalize()} not found",
112-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if entity_type_modifier is not None else None,
110+
entity_id=catalogue_category_id, entity_type=entity_type, use_422=entity_type_modifier is not None
113111
)
114112

115113
def get_breadcrumbs(
@@ -196,7 +194,8 @@ def update(
196194
):
197195
raise DuplicateRecordError(
198196
"Duplicate catalogue category found within the parent catalogue category",
199-
"A catalogue category with the same name already exists within the parent catalogue category",
197+
response_detail="A catalogue category with the same name already exists within the parent catalogue "
198+
"category",
200199
)
201200

202201
# Prevent a catalogue category from being moved to one of its own children
@@ -249,10 +248,7 @@ def delete(self, catalogue_category_id: str, session: Optional[ClientSession] =
249248
session=session,
250249
)
251250
if result.deleted_count == 0:
252-
raise MissingRecordError(
253-
f"No catalogue category found with ID: {catalogue_category_id}",
254-
response_detail="Catalogue category not found",
255-
)
251+
raise MissingRecordError(entity_id=catalogue_category_id, entity_type="catalogue category")
256252

257253
def _is_duplicate_catalogue_category(
258254
self,

inventory_management_system_api/repositories/catalogue_item.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import List, Optional
88

99
from bson import ObjectId
10-
from fastapi import status
1110
from pymongo.client_session import ClientSession
1211
from pymongo.collection import Collection
1312

@@ -73,10 +72,9 @@ def get(
7372

7473
if catalogue_item:
7574
return CatalogueItemOut(**catalogue_item)
75+
7676
raise MissingRecordError(
77-
detail=f"No {entity_type} found with ID: {catalogue_item_id}",
78-
response_detail=f"{entity_type.capitalize()} not found",
79-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if entity_type_modifier is not None else None,
77+
entity_id=catalogue_item_id, entity_type=entity_type, use_422=entity_type_modifier is not None
8078
)
8179

8280
def list(
@@ -143,9 +141,7 @@ def delete(self, catalogue_item_id: str, session: Optional[ClientSession] = None
143141
session=session,
144142
)
145143
if result.deleted_count == 0:
146-
raise MissingRecordError(
147-
f"No catalogue item found with ID: {catalogue_item_id}", response_detail="Catalogue item not found"
148-
)
144+
raise MissingRecordError(entity_id=catalogue_item_id, entity_type="catalogue item")
149145

150146
def has_child_elements(self, catalogue_item_id: str, session: Optional[ClientSession] = None) -> bool:
151147
"""

inventory_management_system_api/repositories/item.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import List, Optional
88

99
from bson import ObjectId
10-
from fastapi import status
1110
from pymongo.client_session import ClientSession
1211
from pymongo.collection import Collection
1312

@@ -70,11 +69,8 @@ def get(
7069

7170
if item:
7271
return ItemOut(**item)
73-
raise MissingRecordError(
74-
detail=f"No {entity_type} found with ID: {item_id}",
75-
response_detail=f"{entity_type.capitalize()} not found",
76-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if entity_type_modifier is not None else None,
77-
)
72+
73+
raise MissingRecordError(entity_id=item_id, entity_type=entity_type, use_422=entity_type_modifier is not None)
7874

7975
def list(
8076
self, system_id: Optional[str], catalogue_item_id: Optional[str], session: Optional[ClientSession] = None
@@ -140,7 +136,7 @@ def delete(self, item_id: str, session: Optional[ClientSession] = None) -> None:
140136
logger.info("Deleting item with ID: %s from the database", item_id)
141137
result = self._items_collection.delete_one({"_id": item_id}, session=session)
142138
if result.deleted_count == 0:
143-
raise MissingRecordError(f"No item found with ID: {str(item_id)}", response_detail="Item not found")
139+
raise MissingRecordError(entity_id=str(item_id), entity_type="item")
144140

145141
def insert_property_to_all_in(
146142
self, catalogue_item_ids: List[ObjectId], property_in: PropertyIn, session: Optional[ClientSession] = None

inventory_management_system_api/repositories/manufacturer.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
from typing import List, Optional
77

8-
from fastapi import status
98
from pymongo.client_session import ClientSession
109
from pymongo.collection import Collection
1110

@@ -79,10 +78,9 @@ def get(
7978

8079
if manufacturer:
8180
return ManufacturerOut(**manufacturer)
81+
8282
raise MissingRecordError(
83-
detail=f"No {entity_type} found with ID: {manufacturer_id}",
84-
response_detail=f"{entity_type.capitalize()} not found",
85-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if entity_type_modifier is not None else None,
83+
entity_id=manufacturer_id, entity_type=entity_type, use_422=entity_type_modifier is not None
8684
)
8785

8886
def list(self, session: Optional[ClientSession] = None) -> List[ManufacturerOut]:
@@ -146,9 +144,7 @@ def delete(self, manufacturer_id: str, session: Optional[ClientSession] = None)
146144
logger.info("Deleting manufacturer with ID: %s from the database", manufacturer_id)
147145
result = self._manufacturers_collection.delete_one({"_id": manufacturer_id}, session=session)
148146
if result.deleted_count == 0:
149-
raise MissingRecordError(
150-
f"No manufacturer found with ID: {str(manufacturer_id)}", response_detail="Manufacturer not found"
151-
)
147+
raise MissingRecordError(entity_id=str(manufacturer_id), entity_type="manufacturer")
152148

153149
def _is_duplicate_manufacturer(
154150
self, code: str, manufacturer_id: Optional[CustomObjectId] = None, session: Optional[ClientSession] = None

inventory_management_system_api/repositories/system.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
from typing import Optional
77

8-
from fastapi import status
98
from pymongo.client_session import ClientSession
109
from pymongo.collection import Collection
1110

@@ -60,7 +59,7 @@ def create(self, system: SystemIn, session: Optional[ClientSession] = None) -> S
6059
if self._is_duplicate_system(parent_id, system.code, session=session):
6160
raise DuplicateRecordError(
6261
"Duplicate system found within the parent system",
63-
"A system with the same name already exists within the parent system",
62+
response_detail="A system with the same name already exists within the parent system",
6463
)
6564

6665
logger.info("Inserting the new system into the database")
@@ -92,11 +91,8 @@ def get(
9291

9392
if system:
9493
return SystemOut(**system)
95-
raise MissingRecordError(
96-
detail=f"No {entity_type} found with ID: {system_id}",
97-
response_detail=f"{entity_type.capitalize()} not found",
98-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if entity_type_modifier is not None else None,
99-
)
94+
95+
raise MissingRecordError(entity_id=system_id, entity_type=entity_type, use_422=entity_type_modifier is not None)
10096

10197
def get_breadcrumbs(self, system_id: str, session: Optional[ClientSession] = None) -> BreadcrumbsGetSchema:
10298
"""
@@ -200,7 +196,7 @@ def delete(self, system_id: str, session: Optional[ClientSession] = None) -> Non
200196
{"_id": CustomObjectId(system_id, entity_type="system", not_found_if_invalid=True)}, session=session
201197
)
202198
if result.deleted_count == 0:
203-
raise MissingRecordError(detail=f"No system found with ID: {system_id}", response_detail="System not found")
199+
raise MissingRecordError(entity_id=system_id, entity_type="system")
204200

205201
def _is_duplicate_system(
206202
self,

inventory_management_system_api/repositories/unit.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
from typing import Optional
77

8-
from fastapi import status
98
from pymongo.client_session import ClientSession
109
from pymongo.collection import Collection
1110

@@ -88,11 +87,8 @@ def get(
8887
unit = self._units_collection.find_one({"_id": unit_id}, session=session)
8988
if unit:
9089
return UnitOut(**unit)
91-
raise MissingRecordError(
92-
detail=f"No {entity_type} found with ID: {unit_id}",
93-
response_detail=f"{entity_type.capitalize()} not found",
94-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if entity_type_modifier is not None else None,
95-
)
90+
91+
raise MissingRecordError(entity_id=unit_id, entity_type=entity_type, use_422=entity_type_modifier is not None)
9692

9793
def delete(self, unit_id: str, session: Optional[ClientSession] = None) -> None:
9894
"""
@@ -115,7 +111,7 @@ def delete(self, unit_id: str, session: Optional[ClientSession] = None) -> None:
115111
logger.info("Deleting unit with ID %s from the database", unit_id)
116112
result = self._units_collection.delete_one({"_id": unit_id}, session=session)
117113
if result.deleted_count == 0:
118-
raise MissingRecordError(f"No unit found with ID: {str(unit_id)}", response_detail="Unit not found")
114+
raise MissingRecordError(entity_id=str(unit_id), entity_type="unit")
119115

120116
def _is_duplicate_unit(
121117
self, code: str, unit_id: CustomObjectId = None, session: Optional[ClientSession] = None

inventory_management_system_api/repositories/usage_status.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
from typing import Optional
77

8-
from fastapi import status
98
from pymongo.client_session import ClientSession
109
from pymongo.collection import Collection
1110

@@ -89,10 +88,9 @@ def get(
8988

9089
if usage_status:
9190
return UsageStatusOut(**usage_status)
91+
9292
raise MissingRecordError(
93-
detail=f"No {entity_type} found with ID: {usage_status_id}",
94-
response_detail=f"{entity_type.capitalize()} not found",
95-
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY if entity_type_modifier is not None else None,
93+
entity_id=usage_status_id, entity_type=entity_type, use_422=entity_type_modifier is not None
9694
)
9795

9896
def delete(self, usage_status_id: str, session: Optional[ClientSession] = None) -> None:
@@ -116,9 +114,7 @@ def delete(self, usage_status_id: str, session: Optional[ClientSession] = None)
116114
logger.info("Deleting usage status with ID %s from the database", usage_status_id)
117115
result = self._usage_statuses_collection.delete_one({"_id": usage_status_id}, session=session)
118116
if result.deleted_count == 0:
119-
raise MissingRecordError(
120-
f"No usage status found with ID: {str(usage_status_id)}", response_detail="Usage status not found"
121-
)
117+
raise MissingRecordError(entity_id=str(usage_status_id), entity_type="usage status")
122118

123119
def _is_duplicate_usage_status(
124120
self, code: str, usage_status_id: Optional[CustomObjectId] = None, session: Optional[ClientSession] = None

inventory_management_system_api/repositories/utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ def compute_breadcrumbs(
110110

111111
result = breadcrumb_query_result[0]["result"]
112112
if len(result) == 0:
113-
raise MissingRecordError(
114-
f"Entity with the ID '{entity_id}' was not found in the collection '{collection_name}'",
115-
response_detail=f"{entity_type.capitalize()} not found",
116-
)
113+
raise MissingRecordError(entity_id=entity_id, entity_type=entity_type)
117114
for element in result:
118115
trail.append((str(element["_id"]), element["name"]))
119116
full_trail = result[0]["parent_id"] is None

inventory_management_system_api/services/catalogue_category_property.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ def update(
202202

203203
if not existing_property_out:
204204
raise MissingRecordError(
205-
f"No property found with ID: {catalogue_category_property_id}",
206-
response_detail="Catalogue category property not found",
205+
entity_id=catalogue_category_property_id, entity_type="catalogue category property"
207206
)
208207

209208
# Modify the name if necessary and check it doesn't cause a conflict

0 commit comments

Comments
 (0)