Skip to content

Commit

Permalink
fix: gracefully handle 404s from web api
Browse files Browse the repository at this point in the history
Closes #98
  • Loading branch information
stdavis committed Dec 17, 2022
1 parent 741bee7 commit 464e938
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"ADDSYSTEM",
"AGRC",
"atch",
"backoff",
"Buildx",
"CAROOT",
"choco",
"cloudrun",
"codecov",
"dotenv",
"forcelist",
"FULLADD",
"geocoder",
"geosearch",
Expand Down
3 changes: 3 additions & 0 deletions src/masquerade/providers/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def make_request(address, zone, out_spatial_reference, max_locations):

response = session.get(url, params=parameters, headers=headers)

if response.status_code == 404 and 'no address candidates found' in response.text.lower():
return []

if response.ok:
try:
result = response.json()['result']
Expand Down
11 changes: 9 additions & 2 deletions tests/test_web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,16 @@ def test_get_address_candidates_raises(requests_mock):


def test_get_address_candidates_bad_address(requests_mock):
requests_mock.get(re.compile(f'{WEB_API_URL}.*'), json={}, status_code=500)
requests_mock.get(
re.compile(f'{WEB_API_URL}.*'),
json={
"status": 404,
"message": "No address candidates found with a score of 70 or better."
},
status_code=404
)

candidates = get_candidates_from_single_line('bad address', 3857, 5)
candidates = get_candidates_from_single_line('123 bad address, city name', 3857, 5)

assert len(candidates) == 0

Expand Down

0 comments on commit 464e938

Please sign in to comment.