Skip to content

Commit

Permalink
fix: log ugrc api key errors. closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdadams committed Jul 8, 2024
1 parent 5d0b6af commit bd9976a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/palletjack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def validate_api_key(api_key):
if response_json["status"] == 200:
return
if response_json["status"] == 400 and "Invalid API key" in response_json["message"]:
module_logger.error(f'API key validation failed: {response_json["message"]}')
raise ValueError(f'API key validation failed: {response_json["message"]}')

warnings.warn(f'Unhandled API key validation response {response_json["status"]}: {response_json["message"]}')
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,18 @@ def test_validate_api_key_bad_key(self, mocker):
with pytest.raises(ValueError, match=re.escape("API key validation failed: Invalid API key")):
palletjack.utils.Geocoding.validate_api_key("foo")

def test_validate_api_key_bad_key_logs(self, mocker, caplog):
req_mock = mocker.patch("palletjack.utils.requests", autospec=True)
response_mock = mocker.Mock()
response_mock.json.return_value = {"status": 400, "message": "Invalid API key"}
req_mock.get.return_value = response_mock

with pytest.raises(ValueError, match=re.escape("API key validation failed: Invalid API key")):
palletjack.utils.Geocoding.validate_api_key("foo")

caplog.set_level(logging.ERROR, logger="utils")
assert "API key validation failed: Invalid API key" in caplog.text

def test_validate_api_key_handles_network_exception(self, mocker, caplog):
req_mock = mocker.patch("palletjack.utils.requests", autospec=True)
mocker.patch("palletjack.utils.sleep")
Expand Down

0 comments on commit bd9976a

Please sign in to comment.