Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #106: improve exception handling in fertiscan #199

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 101 additions & 118 deletions fertiscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@


async def register_analysis(
cursor,
container_client,
cursor: Cursor,
container_client: ContainerClient,
user_id,
hashed_pictures,
analysis_dict,
Expand All @@ -46,44 +46,39 @@ async def register_analysis(
Returns:
- The analysis_dict with the analysis_id added.
"""
try:
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(
f"User not found based on the given id: {user_id}"
)
if not container_client.exists():
raise datastore.ContainerCreationError(
f"Container not found based on the given user_id: {user_id}"
)

# Create picture set for this analysis
picture_set_metadata = data_picture_set.build_picture_set(
user_id, len(hashed_pictures)
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(f"User not found based on the given id: {user_id}")
if not container_client.exists():
raise datastore.ContainerCreationError(
f"Container not found based on the given user_id: {user_id}"
)
picture_set_id = picture.new_picture_set(cursor, picture_set_metadata, user_id)

await datastore.create_picture_set(
cursor, container_client, len(hashed_pictures), user_id, str(picture_set_id)
)
# Create picture set for this analysis
picture_set_metadata = data_picture_set.build_picture_set(
user_id, len(hashed_pictures)
)
picture_set_id = picture.new_picture_set(cursor, picture_set_metadata, user_id)

# Upload pictures to storage
await datastore.upload_pictures(
cursor=cursor,
user_id=user_id,
container_client=container_client,
picture_set_id=picture_set_id,
hashed_pictures=hashed_pictures,
)
await datastore.create_picture_set(
cursor, container_client, len(hashed_pictures), user_id, str(picture_set_id)
)

# Register analysis in the database
formatted_analysis = data_inspection.build_inspection_import(analysis_dict)
# Upload pictures to storage
await datastore.upload_pictures(
cursor=cursor,
user_id=user_id,
container_client=container_client,
picture_set_id=picture_set_id,
hashed_pictures=hashed_pictures,
)

analysis_db = inspection.new_inspection_with_label_info(
cursor, user_id, picture_set_id, formatted_analysis
)
return analysis_db
except data_inspection.MissingKeyError:
raise
# Register analysis in the database
formatted_analysis = data_inspection.build_inspection_import(analysis_dict)

analysis_db = inspection.new_inspection_with_label_info(
cursor, user_id, picture_set_id, formatted_analysis
)
return analysis_db


async def update_inspection(
Expand Down Expand Up @@ -130,7 +125,7 @@ async def update_inspection(


async def get_full_inspection_json(
cursor,
cursor: Cursor,
inspection_id,
user_id=None,
picture_set_id=None,
Expand All @@ -148,85 +143,80 @@ async def get_full_inspection_json(
Returns:
- The inspection json.
"""
try:
if not inspection.is_a_inspection_id(
cursor=cursor, inspection_id=inspection_id
if not inspection.is_a_inspection_id(cursor=cursor, inspection_id=inspection_id):
raise inspection.InspectionNotFoundError(
f"Inspection not found based on the given id: {inspection_id}"
)

# Check Ids
if (
(
picture_set_id is None
or picture_set_id == ""
or not picture.is_a_picture_set_id(
cursor=cursor, picture_set_id=picture_set_id
)
)
or (
user_id is None
or user_id == ""
or not user.is_a_user_id(cursor=cursor, user_id=user_id)
)
or (label_info_id is None or label_info_id == "")
or (company_info_id is None or company_info_id == "")
or (manufacturer_info_id is None or manufacturer_info_id == "")
):
ids = inspection.get_inspection_fk(cursor, inspection_id)
picture_set_id = ids[2]
label_info_id = ids[0]
company_info_id = ids[3]
manufacturer_info_id = ids[4]
user_id = ids[1]
else:
if not picture.is_a_picture_set_id(
cursor=cursor, picture_set_id=picture_set_id
):
raise inspection.InspectionNotFoundError(
f"Inspection not found based on the given id: {inspection_id}"
raise picture.PictureSetNotFoundError(
f"Picture set not found based on the given id: {picture_set_id}"
)

# Check Ids
if (
(
picture_set_id is None
or picture_set_id == ""
or not picture.is_a_picture_set_id(
cursor=cursor, picture_set_id=picture_set_id
)
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(
f"User not found based on the given id: {user_id}"
)
or (
user_id is None
or user_id == ""
or not user.is_a_user_id(cursor=cursor, user_id=user_id)
ids = inspection.get_inspection_fk(cursor, inspection_id)
if not picture_set_id == ids[2]:
raise Warning(
"Picture set id does not match the picture_set_id in the inspection for the given inspection_id"
)
or (label_info_id is None or label_info_id == "")
or (company_info_id is None or company_info_id == "")
or (manufacturer_info_id is None or manufacturer_info_id == "")
):
ids = inspection.get_inspection_fk(cursor, inspection_id)
picture_set_id = ids[2]
label_info_id = ids[0]
company_info_id = ids[3]
manufacturer_info_id = ids[4]
user_id = ids[1]
else:
if not picture.is_a_picture_set_id(
cursor=cursor, picture_set_id=picture_set_id
):
raise picture.PictureSetNotFoundError(
f"Picture set not found based on the given id: {picture_set_id}"
)
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(
f"User not found based on the given id: {user_id}"
)
ids = inspection.get_inspection_fk(cursor, inspection_id)
if not picture_set_id == ids[2]:
raise Warning(
"Picture set id does not match the picture_set_id in the inspection for the given inspection_id"
)
if not label_info_id == ids[0]:
raise Warning(
"Label info id does not match the label_info_id in the inspection for the given inspection_id"
)
if not company_info_id == ids[3]:
raise Warning(
"Company info id does not match the company_info_id in the inspection for the given inspection_id"
)
if not manufacturer_info_id == ids[4]:
raise Warning(
"Manufacturer info id does not match the manufacturer_info_id in the inspection for the given inspection_id"
)
if not user_id == ids[1]:
raise Warning(
"User id does not match the user_id in the inspection for the given inspection_id"
)

# Retrieve pictures
# pictures_ids = picture.get_picture_in_picture_set(cursor, picture_set_id)

# Retrieve label_info
inspection_metadata = data_inspection.build_inspection_export(
cursor, inspection_id, label_info_id
)
if not label_info_id == ids[0]:
raise Warning(
"Label info id does not match the label_info_id in the inspection for the given inspection_id"
)
if not company_info_id == ids[3]:
raise Warning(
"Company info id does not match the company_info_id in the inspection for the given inspection_id"
)
if not manufacturer_info_id == ids[4]:
raise Warning(
"Manufacturer info id does not match the manufacturer_info_id in the inspection for the given inspection_id"
)
if not user_id == ids[1]:
raise Warning(
"User id does not match the user_id in the inspection for the given inspection_id"
)

# Retrieve pictures
# pictures_ids = picture.get_picture_in_picture_set(cursor, picture_set_id)

return inspection_metadata
except inspection.InspectionNotFoundError:
raise
# Retrieve label_info
inspection_metadata = data_inspection.build_inspection_export(
cursor, inspection_id, label_info_id
)

return inspection_metadata


async def get_user_analysis_by_verified(cursor, user_id, verified: bool):
async def get_user_analysis_by_verified(cursor: Cursor, user_id, verified: bool):
"""
This function fetch all the inspection of a user

Expand All @@ -251,16 +241,9 @@ async def get_user_analysis_by_verified(cursor, user_id, verified: bool):
]
"""

try:
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(
f"User not found based on the given id: {user_id}"
)
return inspection.get_all_user_inspection_filter_verified(
cursor, user_id, verified
)
except user.UserNotFoundError:
raise
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(f"User not found based on the given id: {user_id}")
return inspection.get_all_user_inspection_filter_verified(cursor, user_id, verified)


async def delete_inspection(
Expand Down
31 changes: 31 additions & 0 deletions fertiscan/db/metadata/errors.py
Francois-Werbrouck marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Metadata exceptions hierarchy:

Exception
|__MetadataError
|__BuildInspectionImportError
|__BuildInspectionExportError
|__NPKError
"""


class MetadataError(Exception):
"""Base exception for metadata errors"""

pass


class BuildInspectionImportError(MetadataError):
"""Error raised when building the inspection import fails"""

pass


class BuildInspectionExportError(MetadataError):
"""Error raised when building the inspection export fails"""

pass


class NPKError(MetadataError):
pass
Loading
Loading