Skip to content

Commit

Permalink
fix: handle missing keys in batch data
Browse files Browse the repository at this point in the history
Trying to fix these types of errors:
```
File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/site-packages/flask_cors/extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/usr/local/lib/python3.10/site-packages/flask_json.py", line 340, in wrapper2
    rv = f(*args, **kw)
  File "/usr/local/lib/python3.10/site-packages/masquerade/main.py", line 272, in geocode_addresses
    cleanse_text(address['attributes']['Address']), zone, out_spatial_reference
KeyError: 'Address'
```
  • Loading branch information
stdavis committed Jun 20, 2023
1 parent bb1bfa8 commit 301fc0b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/masquerade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def geocode_addresses():
candidate = no_match
else:
candidate['attributes']['ResultID'] = address['attributes']['OBJECTID']
except HTTPError:
except (HTTPError, KeyError):
candidate = no_match

locations.append(candidate)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_masquerade.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,32 @@ def test_can_handle_bad_values(test_client):
response_json = json.loads(response.data)

assert len(response_json['suggestions']) > 0


def test_batch_can_handle_missing_addresses(test_client):
response = test_client.post(
f'{GEOCODE_SERVER_ROUTE}/geocodeAddresses',
data={
'addresses': dumps({
'records': [{
'attributes': {
'OBJECTID': 1,
'Zip': '84043'
}
}, {
'attributes': {
'OBJECTID': 2,
'Zip': '84043'
}
}]
})
}
)

assert response.status_code == 200

response_json = json.loads(response.data)

assert len(response_json['locations']) == 2
assert response_json['locations'][0]['address'] == None
assert response_json['locations'][1]['address'] == None

0 comments on commit 301fc0b

Please sign in to comment.