Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks if parameters are available in error response before accessing… #86

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/currencycloud/errors/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class ApiError(Exception):
class ApiErrorMessage:
def __init__(self, field, error):
self.field = field
self.code = error['code']
self.message = error['message']
self.params = error['params']
self.code = error["code"] if "code" in error else "No Code"
self.message = error["message"] if "message" in error else "No Message"
self.params = error["params"] if "params" in error else {}

def to_h(self):
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"http_interactions": [
{
"recorded_at": "2021-03-23T08:49:58",
"request": {
"body": {
"encoding": "utf-8",
"string": "login_id=development%40currencycloud.com&api_key=deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Content-Length": "104",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "CurrencyCloudSDK/2.0 Python/5.2.0"
},
"method": "POST",
"uri": "https://devapi.currencycloud.com/v2/authenticate/api"
},
"response": {
"url": "https://devapi.currencycloud.com/v2/authenticate/api",
"body": {
"encoding": "utf-8",
"string": "{\"error_code\":\"too_many_requests\",\"error_messages\":{\"base\":[{}]}}"
},
"headers": {
"x-request-id": "9bec876a-8ad8-4644-a8d2-e902105a38bd",
"connection": "close",
"content-type": "application/json;charset=utf-8",
"content-length": "429",
"server": "nginx",
"x-content-type-options": "nosniff",
"date": "Tue, 21 Mar 2023 08:49:58 GMT"
},
"status": {
"code": 429,
"message": "Too Many Requests"
},
"url": "https://devapi.currencycloud.com/v2/authenticate/api"
}
}
],
"recorded_with": "betamax/0.8.0"
}
18 changes: 18 additions & 0 deletions tests/integration/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,21 @@ def test_error_is_handled_different_json_format_2(self):
assert error_message.code == "unknown_error"
assert error_message.message == "Unhandled Error occurred. Check params for details"
assert error_message.params

def test_error_is_handled_missing_params_in_error_message(self):
with Betamax(self.client.config.session) as betamax:
betamax.use_cassette("errors/is_handled_missing_params_in_error_message")

error = None
try:
self.client.auth.authenticate()
raise Exception("Should have failed")
except TooManyRequestsError as e:
error = e

assert len(error.messages) == 1
error_message = error.messages[0]
assert error_message.field == "base"
assert error_message.code == "No Code"
assert error_message.message == "No Message"
assert not error_message.params