Skip to content

Commit

Permalink
Improved logging and error handling.
Browse files Browse the repository at this point in the history
Based on PR review feedback adding additional details to response for
failed barcode validation, additional logging for debugging purposes,
and respone with 500 status code for unidentified errors.
  • Loading branch information
davereinhart committed Aug 24, 2021
1 parent 53052a5 commit c00de80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
29 changes: 18 additions & 11 deletions lib/id3c/api/datastore.py
Expand Up @@ -411,15 +411,16 @@ def store_sample(session: DatabaseSession, sample: dict) -> Any:
result["details"] = f"sample barcode «{sample_barcode}» not found"
elif sample_identifier and sample_identifier.set_use != 'sample':
result["status"] = "validation_failed"
result["details"] = f"barcode «{sample_barcode}» has use type «{sample_identifier.set_use}»"
result["details"] = f"barcode «{sample_barcode}» has use type «{sample_identifier.set_use}» instead of expected use type «sample»"
elif collection_barcode and not collection_identifier:
result["status"] = "validation_failed"
result["details"] = f"collection barcode «{collection_barcode}» not found"
elif collection_identifier and collection_identifier.set_use != 'collection':
result["status"] = "validation_failed"
result["details"] = f"barcode «{collection_barcode}» has use type «{collection_identifier.set_use}»"
result["details"] = f"barcode «{collection_barcode}» has use type «{collection_identifier.set_use}» instead of expected use type «collection»"

if result.get("status", None) == "validation_failed":
LOG.debug(f"Validation failed for {sample} with details: {result.get('details')}")
return result

collected_date = sample.get("collection_date", None)
Expand All @@ -428,16 +429,22 @@ def store_sample(session: DatabaseSession, sample: dict) -> Any:
# the 'sample_barcode' and 'collection_barcode' keys
should_update_identifiers = True if (sample_identifier and collection_identifier) else False

sample, status = upsert_sample(session,
update_identifiers = should_update_identifiers,
identifier = sample_identifier.uuid if sample_identifier else None,
collection_identifier = collection_identifier.uuid if collection_identifier else None,
collection_date = collected_date,
encounter_id = None,
additional_details = sample)
try:
sample, status = upsert_sample(session,
update_identifiers = should_update_identifiers,
identifier = sample_identifier.uuid if sample_identifier else None,
collection_identifier = collection_identifier.uuid if collection_identifier else None,
collection_date = collected_date,
encounter_id = None,
additional_details = sample)

result["sample"] = sample
result["status"] = status
except Exception as e:
result["status"] = "upsert_error"
result["details"] = f"error upserting sample record: {str(e)}"
LOG.debug(f"Error on upsert_sample: {str(e)}")

result["sample"] = sample
result["status"] = status
return result

@export
Expand Down
8 changes: 7 additions & 1 deletion lib/id3c/api/routes.py
Expand Up @@ -329,7 +329,13 @@ def post_sample(*, session):

result = datastore.store_sample(session, sample)

return (jsonify(result), 200) if "sample" in result else (jsonify(result), 400)
status_code = 500
if "sample" in result:
status_code = 200
elif result.get("status") == "validation_failed":
status_code = 400

return (jsonify(result), status_code)

# Load all extra API routes from extensions
# Needs to be at the end of route declarations to allow customization of
Expand Down

0 comments on commit c00de80

Please sign in to comment.