Skip to content

Commit 5e46092

Browse files
authored
Better non-JSON response error handling (#39)
1 parent 98c1ae4 commit 5e46092

8 files changed

Lines changed: 31 additions & 16 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ python:
88
- '3.7'
99
- '3.8'
1010
- '3.9'
11+
- '3.10'
1112
install:
1213
- pip install tox-travis
1314
script:

Changes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
v2.1.0 Thu 13 Oct 2022
2+
Better handling of non-JSON error responses from API
3+
Test on Python 3.10
4+
15
v2.0.0 Thu 15 Jul 2021
26
Python 2 no longer supported
37
New geocode_async and reverse_geocode_async methods

Vagrantfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Vagrant.configure("2") do |config|
55

6-
config.vm.box = "bento/ubuntu-20.04"
6+
config.vm.box = "bento/ubuntu-22.04"
77

88
config.vm.synced_folder ".", "/home/vagrant/python-opencage-geocoder"
99

opencage/geocoder.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ def _opencage_request(self, params):
240240
try:
241241
response_json = response.json()
242242
except ValueError as e:
243-
if response.status_code == 200:
244-
raise UnknownError("Non-JSON result from server") from e
243+
raise UnknownError("Non-JSON result from server") from e
245244

246245
if response.status_code == 401:
247246
raise NotAuthorizedError()
@@ -267,8 +266,7 @@ async def _opencage_async_request(self, params):
267266
try:
268267
response_json = await response.json()
269268
except ValueError as e:
270-
if response.status == 200:
271-
raise UnknownError("Non-JSON result from server") from e
269+
raise UnknownError("Non-JSON result from server") from e
272270

273271
if response.status == 401:
274272
raise NotAuthorizedError()

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828

2929
setup(
3030
name="opencage",
31-
version="2.0.0",
31+
version="2.1.0",
3232
description="Wrapper module for the OpenCage Geocoder API",
3333
long_description=long_description,
3434
long_description_content_type='text/markdown',
3535
author="OpenCage GmbH",
3636
author_email="info@opencagedata.com",
3737
url="https://github.com/OpenCageData/python-opencage-geocoder/",
38-
download_url="https://github.com/OpenCageData/python-opencage-geocoder/tarball/2.0.0",
38+
download_url="https://github.com/OpenCageData/python-opencage-geocoder/tarball/2.1.0",
3939
license="BSD",
4040
packages=find_packages(),
4141
include_package_data=True,
@@ -52,6 +52,7 @@
5252
'Programming Language :: Python :: 3.7',
5353
'Programming Language :: Python :: 3.8',
5454
'Programming Language :: Python :: 3.9',
55+
'Programming Language :: Python :: 3.10',
5556
'Topic :: Scientific/Engineering :: GIS',
5657
'Topic :: Utilities'
5758
],
@@ -63,7 +64,7 @@
6364
test_suite='tests',
6465
tests_require=[
6566
'httpretty>=0.9.6',
66-
'pylint>=2.7.2',
67+
'pylint==2.9.1',
6768
'pytest>=6.0'
6869
],
6970
)

test/test_error_unknown.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,32 @@ def test_http_500_status():
1313
httpretty.register_uri(
1414
httpretty.GET,
1515
geocoder.url,
16-
body='',
1716
status=500,
1817
)
1918

20-
with pytest.raises(UnknownError):
19+
with pytest.raises(UnknownError) as e:
2120
geocoder.geocode('whatever')
2221

22+
assert str(e.value) == '500 status code from API'
23+
2324
@httprettified
2425
def test_non_json():
26+
"These kinds of errors come from webserver and may not be JSON"
2527
httpretty.register_uri(
2628
httpretty.GET,
2729
geocoder.url,
28-
body='',
30+
body='<html><body><h1>503 Service Unavailable</h1></body></html>',
31+
forcing_headers={
32+
'Content-Type': 'text/html',
33+
},
34+
status=503
2935
)
3036

31-
with pytest.raises(UnknownError):
37+
with pytest.raises(UnknownError) as e:
3238
geocoder.geocode('whatever')
3339

40+
assert str(e.value) == 'Non-JSON result from server'
41+
3442
@httprettified
3543
def test_no_results_key():
3644
httpretty.register_uri(
@@ -39,5 +47,7 @@ def test_no_results_key():
3947
body='{"spam": "eggs"}',
4048
)
4149

42-
with pytest.raises(UnknownError):
50+
with pytest.raises(UnknownError) as e:
4351
geocoder.geocode('whatever')
52+
53+
assert str(e.value) == "JSON from API doesn't have a 'results' key"

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py3613, py3710, py38, py3920, lint
2+
envlist = py3613, py3710, py3815, py3920, py3108, lint
33

44
[testenv]
55
deps =
@@ -12,6 +12,6 @@ commands = pytest test
1212
usedevelop = True
1313
deps =
1414
httpretty
15-
pylint
15+
pylint==2.9.1
1616
pytest
1717
commands = pylint opencage examples/demo.py setup.py test

vagrant-provision.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ eval "$(pyenv virtualenv-init -)"
1515
source ~/.bashrc
1616
exec $SHELL
1717

18-
for VERSION in 3.6.13 3.7.10 3.8.8 3.9.2; do
18+
for VERSION in 3.6.13 3.7.10 3.8.15 3.9.2 3.10.8; do
19+
echo "Installing $VERSION ..."
1920
pyenv install --skip-existing $VERSION
2021
pyenv global $VERSION
2122
done

0 commit comments

Comments
 (0)