From d0df249f3e2182006a65d5814d0ece9a1ac2cc81 Mon Sep 17 00:00:00 2001 From: Jonathan Karr Date: Sat, 30 Jan 2021 19:00:34 -0500 Subject: [PATCH] improving handling of API errors --- .../simulator_registry/query.py | 5 +++- .../test_simulator_registry_query.py | 28 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/biosimulators_utils/simulator_registry/query.py b/biosimulators_utils/simulator_registry/query.py index ab029ace..b4dbc1df 100644 --- a/biosimulators_utils/simulator_registry/query.py +++ b/biosimulators_utils/simulator_registry/query.py @@ -6,6 +6,7 @@ :License: MIT """ +from ..biosimulations.utils import validate_biosimulations_api_response from ..config import get_config import requests @@ -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: diff --git a/tests/simulator_registry/test_simulator_registry_query.py b/tests/simulator_registry/test_simulator_registry_query.py index 4e397d7a..248a9c68 100644 --- a/tests/simulator_registry/test_simulator_registry_query.py +++ b/tests/simulator_registry/test_simulator_registry_query.py @@ -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):