From 5872d941c6c66587eede3e479fa3e3e60690e1f4 Mon Sep 17 00:00:00 2001 From: Martijn Vermaat <martijn@vermaat.name> Date: Mon, 21 Dec 2015 12:33:58 +0100 Subject: [PATCH] Degrade gracefully on HTTP error from Entrez --- mutalyzer/ncbi.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/mutalyzer/ncbi.py b/mutalyzer/ncbi.py index c4fc77bc..15fcc7c2 100644 --- a/mutalyzer/ncbi.py +++ b/mutalyzer/ncbi.py @@ -3,6 +3,8 @@ Communication with the NCBI. """ +from urllib2 import HTTPError + from Bio import Entrez from .config import settings @@ -65,7 +67,11 @@ def _get_link_from_ncbi(source_db, target_db, match_link_name, source = '%s.%d' % (source_accession, source_version) # Find source record. - handle = Entrez.esearch(db=source_db, term=source) + try: + handle = Entrez.esearch(db=source_db, term=source) + except HTTPError: + return fail_or_retry() + try: result = Entrez.read(handle) except Entrez.Parser.ValidationError: @@ -79,7 +85,11 @@ def _get_link_from_ncbi(source_db, target_db, match_link_name, return fail_or_retry() # Find link from source record to target record. - handle = Entrez.elink(dbfrom=source_db, db=target_db, id=source_gi) + try: + handle = Entrez.elink(dbfrom=source_db, db=target_db, id=source_gi) + except HTTPError: + return fail_or_retry() + try: result = Entrez.read(handle) except Entrez.Parser.ValidationError: @@ -98,8 +108,12 @@ def _get_link_from_ncbi(source_db, target_db, match_link_name, return fail_or_retry() # Get target record. - handle = Entrez.efetch( - db=target_db, id=target_gi, rettype='acc', retmode='text') + try: + handle = Entrez.efetch( + db=target_db, id=target_gi, rettype='acc', retmode='text') + except HTTPError: + return fail_or_retry() + target = unicode(handle.read()).strip().split('.') handle.close() -- GitLab