Skip to content

Commit

Permalink
implement some PR Request Changes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
asuresh-code committed Dec 3, 2024
1 parent 6bc6009 commit 75d9f8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
21 changes: 18 additions & 3 deletions object_storage_api/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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"
7 changes: 3 additions & 4 deletions object_storage_api/repositories/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down
2 changes: 1 addition & 1 deletion object_storage_api/routers/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 75d9f8c

Please sign in to comment.