Skip to content

Commit

Permalink
Add WriteConflictError to routers #417
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Dec 5, 2024
1 parent e134cf9 commit 58a1e87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 19 additions & 0 deletions inventory_management_system_api/routers/v1/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion inventory_management_system_api/routers/v1/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 58a1e87

Please sign in to comment.