Skip to content

Commit f776aa3

Browse files
committed
Clean up duplicate error messages and update repo get types #74
1 parent a6e69a0 commit f776aa3

File tree

16 files changed

+33
-109
lines changed

16 files changed

+33
-109
lines changed

inventory_management_system_api/core/database.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def start_session_transaction(action_description: str) -> Generator[ClientSessio
5151
except OperationFailure as exc:
5252
if "write conflict" in str(exc).lower():
5353
raise WriteConflictError(
54-
f"Write conflict while {action_description}. Please try again later.",
55-
response_detail=f"Write conflict while {action_description}. Please try again later.",
54+
f"Write conflict while {action_description}. Please try again later."
5655
) from exc
5756
raise exc
5857

inventory_management_system_api/core/exceptions.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def __init__(self, detail: str, response_detail: Optional[str] = None, status_co
3333

3434
if response_detail is not None:
3535
self.response_detail = response_detail
36+
# If there is no response detail defined just use the detail
37+
elif not hasattr(self, "response_detail"):
38+
self.response_detail = detail
3639
if status_code is not None:
3740
self.status_code = status_code
3841

@@ -81,16 +84,6 @@ class InvalidPropertyTypeError(BaseAPIException):
8184

8285
status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
8386

84-
def __init__(self, detail: str):
85-
"""
86-
Initialise the exception.
87-
88-
:param detail: Specific detail of the exception (just like Exception would take).
89-
"""
90-
super().__init__(detail)
91-
92-
self.response_detail = detail
93-
9487

9588
class MissingMandatoryProperty(BaseAPIException):
9689
"""
@@ -99,16 +92,6 @@ class MissingMandatoryProperty(BaseAPIException):
9992

10093
status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
10194

102-
def __init__(self, detail: str):
103-
"""
104-
Initialise the exception.
105-
106-
:param detail: Specific detail of the exception (just like Exception would take).
107-
"""
108-
super().__init__(detail)
109-
110-
self.response_detail = detail
111-
11295

11396
class DuplicateRecordError(DatabaseError):
11497
"""The record being added to the database is a duplicate."""
@@ -183,7 +166,6 @@ class InvalidActionError(BaseAPIException):
183166
"""
184167

185168
status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
186-
response_detail = "Invalid action"
187169

188170

189171
class WriteConflictError(DatabaseError):
@@ -216,13 +198,3 @@ class PropertyValueError(BaseAPIException, ValueError):
216198
"""Exception raised when there is an error caused by a property value"""
217199

218200
status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
219-
220-
def __init__(self, detail: str):
221-
"""
222-
Initialise the exception.
223-
224-
:param detail: Specific detail of the exception (just like Exception would take).
225-
"""
226-
super().__init__(detail)
227-
228-
self.response_detail = detail

inventory_management_system_api/repositories/catalogue_category.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def get(
8484
catalogue_category_id: str,
8585
entity_type_modifier: Optional[str] = None,
8686
session: Optional[ClientSession] = None,
87-
) -> Optional[CatalogueCategoryOut]:
87+
) -> CatalogueCategoryOut:
8888
"""
8989
Retrieve a catalogue category by its ID from a MongoDB database.
9090

@@ -213,10 +213,7 @@ def update(
213213
)
214214
)
215215
):
216-
raise InvalidActionError(
217-
"Cannot move a catalogue category to one of its own children",
218-
response_detail="Cannot move a catalogue category to one of its own children",
219-
)
216+
raise InvalidActionError("Cannot move a catalogue category to one of its own children")
220217

221218
logger.info("Updating catalogue category with ID: %s in the database", catalogue_category_id)
222219
self._catalogue_categories_collection.update_one(

inventory_management_system_api/repositories/catalogue_item.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def get(
5252
catalogue_item_id: str,
5353
entity_type_modifier: Optional[str] = None,
5454
session: Optional[ClientSession] = None,
55-
) -> Optional[CatalogueItemOut]:
55+
) -> CatalogueItemOut:
5656
"""
5757
Retrieve a catalogue item by its ID from a MongoDB database.
5858

@@ -61,7 +61,7 @@ def get(
6161
e.g. parent if its for a parent system.
6262
:param session: PyMongo ClientSession to use for database operations
6363
:return: The retrieved catalogue item, or `None` if not found.
64-
:raises MissingRecordError: If the supplied `system_id` is non-existent.
64+
:raises MissingRecordError: If the supplied `catalogue_item_id` is non-existent.
6565
"""
6666
entity_type = f"{entity_type_modifier} catalogue item" if entity_type_modifier else "catalogue item"
6767
catalogue_item_id = CustomObjectId(

inventory_management_system_api/repositories/item.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ def create(self, item: ItemIn, session: Optional[ClientSession] = None) -> ItemO
5050

5151
def get(
5252
self, item_id: str, entity_type_modifier: Optional[str] = None, session: Optional[ClientSession] = None
53-
) -> Optional[ItemOut]:
53+
) -> ItemOut:
5454
"""
5555
Retrieve an item by its ID from a MongoDB database.
5656

5757
:param item_id: The ID of the item to retrieve
5858
:param entity_type_modifier: String value to put at the start of the entity type used in error messages
5959
e.g. specified if its for a specified item.
6060
:param session: PyMongo ClientSession to use for database operations
61-
:return: The retrieved item, or `None` if not found.
61+
:return: The retrieved item.
62+
:raises MissingRecordError: If the supplied `item_id` is non-existent.
6263
"""
6364

6465
entity_type = f"{entity_type_modifier} item" if entity_type_modifier else "item"

inventory_management_system_api/repositories/manufacturer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def create(self, manufacturer: ManufacturerIn, session: Optional[ClientSession]
5757

5858
def get(
5959
self, manufacturer_id: str, entity_type_modifier: Optional[str] = None, session: Optional[ClientSession] = None
60-
) -> Optional[ManufacturerOut]:
60+
) -> ManufacturerOut:
6161
"""
6262
Retrieve a manufacturer by its ID from a MondoDB database.
6363

inventory_management_system_api/repositories/system.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def create(self, system: SystemIn, session: Optional[ClientSession] = None) -> S
7070

7171
def get(
7272
self, system_id: str, entity_type_modifier: Optional[str] = None, session: Optional[ClientSession] = None
73-
) -> Optional[SystemOut]:
73+
) -> SystemOut:
7474
"""
7575
Retrieve a system by its ID from a MongoDB database
7676

@@ -177,10 +177,7 @@ def update(self, system_id: str, system: SystemIn, session: Optional[ClientSessi
177177
)
178178
)
179179
):
180-
raise InvalidActionError(
181-
"Cannot move a system to one of its own children",
182-
response_detail="Cannot move a system to one of its own children",
183-
)
180+
raise InvalidActionError("Cannot move a system to one of its own children")
184181

185182
logger.info("Updating system with ID: %s in the database", system_id)
186183
self._systems_collection.update_one({"_id": system_id}, {"$set": system.model_dump()}, session=session)

inventory_management_system_api/repositories/unit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def list(self, session: Optional[ClientSession] = None) -> list[UnitOut]:
6969

7070
def get(
7171
self, unit_id: str, entity_type_modifier: Optional[str] = None, session: Optional[ClientSession] = None
72-
) -> Optional[UnitOut]:
72+
) -> UnitOut:
7373
"""
7474
Retrieve a Unit by its ID from a MongoDB database.
7575

@@ -78,7 +78,7 @@ def get(
7878
e.g. specified if its for a specified unit.
7979
:param session: PyMongo ClientSession to use for database operations
8080
:return: The retrieved unit.
81-
:raises MissingRecordError: If the supplied `system_id` is non-existent.
81+
:raises MissingRecordError: If the supplied `unit_id` is non-existent.
8282
"""
8383

8484
entity_type = f"{entity_type_modifier} unit" if entity_type_modifier else "unit"

inventory_management_system_api/repositories/usage_status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def list(self, session: Optional[ClientSession] = None) -> list[UsageStatusOut]:
6767

6868
def get(
6969
self, usage_status_id: str, entity_type_modifier: Optional[str] = None, session: Optional[ClientSession] = None
70-
) -> Optional[UsageStatusOut]:
70+
) -> UsageStatusOut:
7171
"""
7272
Retrieve a usage status by its ID from a MongoDB database.
7373

inventory_management_system_api/services/catalogue_category_property.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,14 @@ def create(
7878
# Mandatory properties must have a default value that is not None as they would need to be
7979
# populated down the subtree
8080
if catalogue_category_property.mandatory and catalogue_category_property.default_value is None:
81-
raise InvalidActionError(
82-
"Cannot add a mandatory property without a default value",
83-
response_detail="Cannot add a mandatory property without a default value",
84-
)
81+
raise InvalidActionError("Cannot add a mandatory property without a default value")
8582

8683
# Obtain the existing catalogue category to validate against
8784
stored_catalogue_category = self._catalogue_category_repository.get(catalogue_category_id)
8885

8986
# Must be a leaf catalogue category in order to have properties
9087
if not stored_catalogue_category.is_leaf:
91-
raise InvalidActionError(
92-
"Cannot add a property to a non-leaf catalogue category",
93-
response_detail="Cannot add a property to a non-leaf catalogue category",
94-
)
88+
raise InvalidActionError("Cannot add a property to a non-leaf catalogue category")
9589

9690
# Ensure the property is actually valid
9791
utils.check_duplicate_property_names(stored_catalogue_category.properties + [catalogue_category_property])
@@ -156,34 +150,23 @@ def _check_valid_allowed_values_update(
156150

157151
# Prevent adding allowed_values to an existing property
158152
if existing_allowed_values is None and new_allowed_values is not None:
159-
raise InvalidActionError(
160-
"Cannot add allowed_values to an existing property",
161-
response_detail="Cannot add allowed_values to an existing property",
162-
)
153+
raise InvalidActionError("Cannot add allowed_values to an existing property")
163154

164155
# Prevent removing allowed_values from an existing property
165156
if existing_allowed_values is not None and new_allowed_values is None:
166-
raise InvalidActionError(
167-
"Cannot remove allowed_values from an existing property",
168-
response_detail="Cannot remove allowed_values from an existing property",
169-
)
157+
raise InvalidActionError("Cannot remove allowed_values from an existing property")
170158

171159
# Prevent changing an allowed_values' type
172160
if existing_allowed_values.type != new_allowed_values.type:
173-
raise InvalidActionError(
174-
"Cannot modify a properties' allowed_values to have a different type",
175-
response_detail="Cannot modify a properties' allowed_values to have a different type",
176-
)
161+
raise InvalidActionError("Cannot modify a properties' allowed_values to have a different type")
177162

178163
# Ensure that a list type adds to the existing values (order doesn't matter)
179164
if existing_allowed_values.type == "list":
180165
for existing_value in existing_allowed_values.values:
181166
if existing_value not in new_allowed_values.values:
182167
raise InvalidActionError(
183168
"Cannot modify existing values inside allowed_values of type 'list', you may only add more "
184-
"values",
185-
response_detail="Cannot modify existing values inside allowed_values of type 'list', you may "
186-
"only add more values",
169+
"values"
187170
)
188171

189172
def update(

0 commit comments

Comments
 (0)