diff --git a/src/opencloning/ncbi_requests.py b/src/opencloning/ncbi_requests.py index 404535c..95b9714 100644 --- a/src/opencloning/ncbi_requests.py +++ b/src/opencloning/ncbi_requests.py @@ -9,7 +9,10 @@ async def async_get(url, headers, params=None) -> Response: async with get_http_client() as client: - return await client.get(url, headers=headers, params=params, timeout=20.0) + resp = await client.get(url, headers=headers, params=params, timeout=20.0) + if resp.status_code == 500: + raise HTTPException(503, 'NCBI is down, try again later') + return resp # TODO: this does not return old assembly accessions, see https://github.com/ncbi/datasets/issues/380#issuecomment-2231142816 @@ -120,9 +123,9 @@ async def get_genbank_sequence(sequence_accession, start=None, end=None, strand= elif resp.status_code == 400: raise HTTPException(404, 'wrong sequence accession') elif resp.status_code == 503: - raise HTTPException(503, 'NCBI returned an error') + raise HTTPException(503, 'NCBI returned an internal server error') else: - raise HTTPException(500, 'NCBI returned an unexpected error') + raise HTTPException(503, 'NCBI returned an unexpected error') def validate_coordinates_pre_request(start, end, strand): diff --git a/tests/test_endpoints_external_import.py b/tests/test_endpoints_external_import.py index 68655ca..3e91695 100644 --- a/tests/test_endpoints_external_import.py +++ b/tests/test_endpoints_external_import.py @@ -936,6 +936,18 @@ def test_exceptions(self): response = client.post('/genome_coordinates', json=viral_source.model_dump()) self.assertStatusCode(response.status_code, 400) + def test_ncbi_down(self): + correct_source = GenomeCoordinatesSource.model_validate( + request_examples.genome_region_examples['full']['value'] + ) + with respx.mock: + respx.get( + 'https://api.ncbi.nlm.nih.gov/datasets/v2alpha/genome/accession/GCF_000002945.2/annotation_report' + ).mock(return_value=httpx.Response(500, text='NCBI is down')) + response = client.post('/genome_coordinates', json=correct_source.model_dump()) + self.assertEqual(response.status_code, 503) + self.assertIn('NCBI is down', response.json()['detail']) + class SEVASourceTest(unittest.TestCase): def test_seva_url(self): diff --git a/tests/test_ncbi_requests.py b/tests/test_ncbi_requests.py index 5699aae..51bcd82 100644 --- a/tests/test_ncbi_requests.py +++ b/tests/test_ncbi_requests.py @@ -17,12 +17,18 @@ async def test_get_genbank_sequence_subset(self): with pytest.raises(HTTPException) as e: await ncbi_requests.get_genbank_sequence('blah', 1, 10, 1) assert e.value.status_code == 503 - assert e.value.detail == 'NCBI returned an error' + assert e.value.detail == 'NCBI returned an internal server error' respx.get('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi').respond(500, text='') with pytest.raises(HTTPException) as e: await ncbi_requests.get_genbank_sequence('blah', 1, 10, 1) - assert e.value.status_code == 500 + assert e.value.status_code == 503 + assert e.value.detail == 'NCBI is down, try again later' + + respx.get('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi').respond(504, text='') + with pytest.raises(HTTPException) as e: + await ncbi_requests.get_genbank_sequence('blah', 1, 10, 1) + assert e.value.status_code == 503 assert e.value.detail == 'NCBI returned an unexpected error' async def test_get_annotations_from_query(self):