Skip to content

Commit

Permalink
feat: add suggested_user_message to SDK v2 err response
Browse files Browse the repository at this point in the history
feat: add suggested_user_message to SDK v2 err response
  • Loading branch information
hhovsepi authored Apr 24, 2024
2 parents 2708e83 + 52b6b01 commit 6e74f0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions smartcar/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def exception_factory(status_code: int, headers: dict, body: str):
resolution=response.get("resolution"),
detail=response.get("detail"),
retry_after=headers.get("Retry-After"),
suggested_user_message=response.get("suggestedUserMessage"),
)
else:
return SmartcarException(
Expand All @@ -99,6 +100,7 @@ def exception_factory(status_code: int, headers: dict, body: str):
doc_url=response.get("docURL"),
resolution=response.get("resolution"),
detail=response.get("detail"),
suggested_user_message=response.get("suggestedUserMessage"),
)

# Weird...
Expand Down
19 changes: 17 additions & 2 deletions tests/e2e/test_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def test_out_of_permission_scope(ford_car):
except Exception as e:
assert isinstance(e, SmartcarException)

# 8 fields stated in exception.py + 'message'
assert len(e.__dict__.keys()) == 9
# 9 fields stated in exception.py + 'message'
assert len(e.__dict__.keys()) == 10
assert e.status_code == 403
assert e.code is None

Expand Down Expand Up @@ -163,3 +163,18 @@ def test_retry_after_found():
except Exception as e:
assert isinstance(e, SmartcarException)
assert e.retry_after == 5000


def test_suggested_user_message():
"""
test that we can get the retry_after amount
"""
try:
raise exception_factory(
429,
{"Retry-After": 5000, "Content-Type": "application/json"},
'{"statusCode":429,"type":"RATE_LIMIT","code":"Vehicle","resolution":{"type":"RETRY_LATER"},"requestId":"e0027f5f-4411-4247-a54d-e34c157d84c1", "suggestedUserMessage": "Please try again later."}',
)
except Exception as e:
assert isinstance(e, SmartcarException)
assert e.suggested_user_message == "Please try again later."

0 comments on commit 6e74f0b

Please sign in to comment.