diff --git a/object_storage_api/core/exceptions.py b/object_storage_api/core/exceptions.py index 785f5b1..6aca36c 100644 --- a/object_storage_api/core/exceptions.py +++ b/object_storage_api/core/exceptions.py @@ -49,9 +49,23 @@ class InvalidObjectIdError(DatabaseError): The provided value is not a valid ObjectId. """ - status_code = 422 + status_code = 404 response_detail = "Invalid ID given" + def __init__(self, detail: str, response_detail: Optional[str] = None, entity_name: Optional[str] = None): + """ + Initialise the exception. + + :param detail: Specific detail of the exception (just like Exception would take - this will only be logged + and not returned in a response). + :param response_detail: Generic detail of the exception to be returned in the response. + :param entity_name: Name of the entity to include in the response detail. + """ + super().__init__(detail, response_detail) + + if entity_name is not None: + self.response_detail = f"{entity_name.capitalize()} not found" + class InvalidImageFileError(BaseAPIException): """ @@ -70,15 +84,16 @@ class MissingRecordError(DatabaseError): status_code = 404 response_detail = "Requested record was not found" - def __init__(self, detail: str, entity_name: Optional[str] = None): + def __init__(self, detail: str, response_detail: Optional[str] = None, entity_name: Optional[str] = None): """ Initialise the exception. :param detail: Specific detail of the exception (just like Exception would take - this will only be logged and not returned in a response). + :param response_detail: Generic detail of the exception to be returned in the response. :param entity_name: Name of the entity to include in the response detail. """ - super().__init__(detail) + super().__init__(detail, response_detail) if entity_name is not None: self.response_detail = f"{entity_name.capitalize()} not found" diff --git a/object_storage_api/repositories/image.py b/object_storage_api/repositories/image.py index 675a6f2..5c434c4 100644 --- a/object_storage_api/repositories/image.py +++ b/object_storage_api/repositories/image.py @@ -54,16 +54,15 @@ def get(self, image_id: str, session: ClientSession = None) -> ImageOut: :raises InvalidObjectIdError: If the supplied `image_id` is invalid. """ logger.info("Retrieving image with ID: %s from the database", image_id) + entity_name = "image" try: image_id = CustomObjectId(image_id) image = self._images_collection.find_one({"_id": image_id}, session=session) except InvalidObjectIdError as exc: - exc.status_code = 404 - exc.response_detail = "Image not found" - raise exc + raise InvalidObjectIdError(detail=f"Invalid ObjectId value '{image_id}'", entity_name=entity_name) from exc if image: return ImageOut(**image) - raise MissingRecordError(detail=f"Image with image_id {image_id} was not found.", entity_name="image") + raise MissingRecordError(detail=f"No image found with ID: {image_id}", entity_name=entity_name) def list(self, entity_id: Optional[str], primary: Optional[bool], session: ClientSession = None) -> list[ImageOut]: """ diff --git a/object_storage_api/routers/image.py b/object_storage_api/routers/image.py index 68d28e0..9e90d69 100644 --- a/object_storage_api/routers/image.py +++ b/object_storage_api/routers/image.py @@ -74,6 +74,6 @@ def get_image( image_id: Annotated[str, Path(description="ID of the image to get")], image_service: ImageServiceDep ) -> ImageSchema: # pylint: disable=missing-function-docstring - logger.info("Getting image with ID %s", image_id) + logger.info("Getting image with ID: %s", image_id) return image_service.get(image_id)