From 58a1e873cbfd69112499a4e1139cbdbee99d09ff Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Thu, 5 Dec 2024 09:05:32 +0000 Subject: [PATCH] Add WriteConflictError to routers #417 --- .../routers/v1/item.py | 19 +++++++++++++++++++ .../routers/v1/setting.py | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/inventory_management_system_api/routers/v1/item.py b/inventory_management_system_api/routers/v1/item.py index e806d402..aa692113 100644 --- a/inventory_management_system_api/routers/v1/item.py +++ b/inventory_management_system_api/routers/v1/item.py @@ -14,6 +14,7 @@ InvalidPropertyTypeError, MissingMandatoryProperty, MissingRecordError, + WriteConflictError, ) from inventory_management_system_api.schemas.item import ItemPatchSchema, ItemPostSchema, ItemSchema from inventory_management_system_api.services.item import ItemService @@ -58,6 +59,12 @@ def create_item(item: ItemPostSchema, item_service: ItemServiceDep) -> ItemSchem message = "Unable to create item" logger.exception(message) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc + # pylint: disable=duplicate-code + except WriteConflictError as exc: + message = str(exc) + logger.exception(message) + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=message) from exc + # pylint: enable=duplicate-code @router.delete( @@ -77,6 +84,12 @@ def delete_item( message = "Item not found" logger.exception(message) raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=message) from exc + # pylint: disable=duplicate-code + except WriteConflictError as exc: + message = str(exc) + logger.exception(message) + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=message) from exc + # pylint: enable=duplicate-code @router.get(path="", summary="Get items", response_description="List of items") @@ -161,3 +174,9 @@ def partial_update_item( message = "Cannot change the catalogue item of an item" logger.exception(message) raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=message) from exc + # pylint: disable=duplicate-code + except WriteConflictError as exc: + message = str(exc) + logger.exception(message) + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=message) from exc + # pylint: enable=duplicate-code diff --git a/inventory_management_system_api/routers/v1/setting.py b/inventory_management_system_api/routers/v1/setting.py index c3b44fda..d7f3a5b2 100644 --- a/inventory_management_system_api/routers/v1/setting.py +++ b/inventory_management_system_api/routers/v1/setting.py @@ -7,7 +7,7 @@ from fastapi import APIRouter, Depends, HTTPException, status -from inventory_management_system_api.core.exceptions import InvalidObjectIdError, MissingRecordError +from inventory_management_system_api.core.exceptions import InvalidObjectIdError, MissingRecordError, WriteConflictError from inventory_management_system_api.schemas.setting import SparesDefinitionPutSchema, SparesDefinitionSchema from inventory_management_system_api.services.setting import SettingService @@ -37,3 +37,9 @@ def update_spares_definition( message = "A specified usage status does not exist" logger.exception(message) raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=message) from exc + # pylint: disable=duplicate-code + except WriteConflictError as exc: + message = str(exc) + logger.exception(message) + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=message) from exc + # pylint: enable=duplicate-code