Skip to content

Commit

Permalink
produces more informative errors for unsuccessful requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xenatisch committed Aug 6, 2020
1 parent 77df204 commit e092e38
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
25 changes: 25 additions & 0 deletions tests/test_api_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from urllib.parse import unquote
from tempfile import gettempdir
from os.path import join as path_join
import re

# 3rd party:

# Internal:
from uk_covid19 import Cov19API
from uk_covid19.exceptions import FailedRequestError

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -179,3 +181,26 @@ def test_length_equal(self):

self.assertTrue(csv_len == json_len)
self.assertTrue(csv_len == xml_len)

def test_unsuccessful_request(self):
bad_structure = {
"name": "areaName",
"date": "date",
# Missing the trailing "Date" - making the
# metric name invalid.
"newCases": "newCasesBySpecimen"
}

pattern = re.compile(r"404\s-\sNot Found.*'newCasesBySpecimenDate'", re.S | re.M)

api = Cov19API(filters=test_filters, structure=bad_structure)

with self.assertRaisesRegex(FailedRequestError, pattern):
api.get_json()

with self.assertRaisesRegex(FailedRequestError, pattern):
api.get_xml()

with self.assertRaisesRegex(FailedRequestError, pattern):
api.get_csv()

2 changes: 1 addition & 1 deletion uk_covid19/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
__copyright__ = "Copyright (c) 2020, Public Health England"
__description__ = "SDK for the COVID-19 API (Coronavirus Dashboard in the UK)"
__license__ = "MIT"
__version__ = "1.1.4"
__version__ = "1.1.5"
__url__ = "https://coronavirus.data.gov.uk/"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 3 additions & 1 deletion uk_covid19/api_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

# Internal:
from uk_covid19.utils import save_data
from uk_covid19.exceptions import FailedRequestError

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -224,7 +225,8 @@ def _get(self, format_as: str) -> Iterator[Response]:
with request("GET", self.endpoint, params=api_params,
verify=certifi.where()) as response:
if response.status_code >= HTTPStatus.BAD_REQUEST:
raise RuntimeError(f'Request failed: {response.text}')
raise FailedRequestError(response=response, params=api_params)

if response.status_code == HTTPStatus.NO_CONTENT:
self.total_pages = api_params["page"] - 1
break
Expand Down
55 changes: 55 additions & 0 deletions uk_covid19/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin python3

# Imports
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Python:
from pprint import pformat
from urllib.parse import unquote

# 3rd party:
from requests import Response

# Internal:

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

__all__ = [
'FailedRequestError'
]


class FailedRequestError(RuntimeError):
"""
Exception for failed HTTP request.
"""

message = """
Request failed .... {status_code} - {reason}
Response .......... {response_text}
URL ............... {url}
Decoded URL ........{decoded_url}
Parameters:
{params}
"""

def __init__(self, response: Response, params: dict):
"""
Parameters
----------
response: Response
HTTP request ``Response`` object, as produced by the ``requests``
library.
params: dict
Dictionary of parameters.
"""
message = self.message.format(
status_code=response.status_code,
reason=response.reason,
response_text=response.content.decode() or "No response",
url=response.url,
decoded_url=unquote(response.url),
params=pformat(params, indent=2)
)

super().__init__(message)

0 comments on commit e092e38

Please sign in to comment.