Skip to content

Commit

Permalink
improving handling of API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Jan 31, 2021
1 parent 3efc4cc commit d0df249
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 4 additions & 1 deletion biosimulators_utils/simulator_registry/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:License: MIT
"""

from ..biosimulations.utils import validate_biosimulations_api_response
from ..config import get_config
import requests

Expand All @@ -25,8 +26,10 @@ def get_simulator_version_specs(id):

endpoint = get_config().BIOSIMULATORS_API_ENDPOINT
response = requests.get('{}simulators/{}'.format(endpoint, id))

try:
response.raise_for_status()
intro_failure_msg = "The specifications of the versions of `{}` could not be retrieved from the BioSimulators registry.".format(id)
validate_biosimulations_api_response(response, intro_failure_msg)
version_specs = response.json()
except requests.exceptions.HTTPError:
if response.status_code != 404:
Expand Down
28 changes: 26 additions & 2 deletions tests/simulator_registry/test_simulator_registry_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,37 @@ def raise_for_status():
raise requests.exceptions.HTTPError()

def requests_get(self):
return mock.Mock(status_code=404, raise_for_status=raise_for_status)
return mock.Mock(
status_code=404,
raise_for_status=raise_for_status,
json=lambda: {
'error': [
{
'status': 404,
'title': 'Error',
'detail': 'error',
},
],
},
)

with mock.patch('requests.get', side_effect=requests_get):
self.assertEqual(get_simulator_version_specs('tellurium'), [])

def requests_get(self):
return mock.Mock(status_code=400, raise_for_status=raise_for_status)
return mock.Mock(
status_code=400,
raise_for_status=raise_for_status,
json=lambda: {
'error': [
{
'status': 400,
'title': 'Error',
'detail': 'error',
},
],
},
)

with mock.patch('requests.get', side_effect=requests_get):
with self.assertRaises(requests.exceptions.HTTPError):
Expand Down

0 comments on commit d0df249

Please sign in to comment.