Skip to content

Commit 07e7d71

Browse files
authored
Merge pull request #464 from VariantEffect/feature/bencap463/404-response-for-no-matching-caids
Enhance Clingen Allele ID lookup with logging and error handling
2 parents 0c1b391 + 1a37184 commit 07e7d71

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/mavedb/routers/variants.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def lookup_variants(
3434
db: Session = Depends(deps.get_db),
3535
user_data: UserData = Depends(get_current_user),
3636
):
37+
save_to_logging_context({"requested_resource": "clingen-allele-id-lookups"})
38+
save_to_logging_context({"clingen_allele_ids_to_lookup": request.clingen_allele_ids})
39+
logger.debug(msg="Looking up variants by Clingen Allele IDs", extra=logging_context())
40+
3741
variants = db.execute(
3842
select(Variant, MappedVariant.clingen_allele_id)
3943
.join(MappedVariant)
@@ -42,12 +46,25 @@ def lookup_variants(
4246
).all()
4347

4448
variants_by_allele_id: dict[str, list[Variant]] = {allele_id: [] for allele_id in request.clingen_allele_ids}
49+
save_to_logging_context({"num_variants_matching_clingen_allele_ids": len(variants)})
50+
logger.debug(msg="Found variants with matching ClinGen Allele IDs", extra=logging_context())
4551

52+
num_variants_matching_clingen_allele_ids_and_permitted = 0
4653
for variant, allele_id in variants:
4754
if has_permission(user_data, variant.score_set, Action.READ).permitted:
4855
variants_by_allele_id[allele_id].append(variant)
56+
num_variants_matching_clingen_allele_ids_and_permitted += 1
57+
58+
save_to_logging_context(
59+
{"clingen_allele_ids_with_permitted_variants": num_variants_matching_clingen_allele_ids_and_permitted}
60+
)
61+
62+
if not any(matched_variants for matched_variants in variants_by_allele_id.values()):
63+
logger.info(msg="No variants found for the provided Clingen Allele IDs.", extra=logging_context())
64+
raise HTTPException(status_code=404, detail="No variants found for the provided Clingen Allele IDs.")
4965

50-
return [variants_by_allele_id[allele_id] for allele_id in request.clingen_allele_ids]
66+
# These dict methods will preserve key ordering.
67+
return list(variants_by_allele_id.values())
5168

5269

5370
@router.get(

0 commit comments

Comments
 (0)