Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Add error improvements and bump 0.6.2
Browse files Browse the repository at this point in the history
Add error improvements
  • Loading branch information
andrewsayre committed Feb 15, 2019
2 parents 0fe7dec + e0c8fdf commit 071e978
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pysmartthings/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Define consts for the pysmartthings package."""

__title__ = "pysmartthings"
__version__ = "0.6.1"
__version__ = "0.6.2"
13 changes: 13 additions & 0 deletions pysmartthings/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ def __init__(self, request_info, history, *, status=None, message='',
"""Create a new instance of the API Error."""
super().__init__(request_info, history, status=status,
message=message, headers=headers)
self._raw_error_response = data
self._request_id = data.get('requestId')
self._error = APIErrorDetail(data['error'])

@property
def raw_error_response(self):
"""Get the raw error response returned."""
return self._raw_error_response

@property
def request_id(self) -> Optional[str]:
"""Get request correlation id."""
Expand All @@ -68,6 +74,13 @@ def error(self) -> APIErrorDetail:
"""Get the API error document."""
return self._error

def is_target_error(self):
"""Determine if the error is due to an issue with the target."""
return self.error.code == 'ConstraintViolationError' \
and len(self.error.details) == 1 \
and self.error.details[0].code \
and self.error.details[0].code.startswith('Target')


class APIInvalidGrant(Exception):
"""Define an invalid grant error."""
Expand Down
40 changes: 40 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Tests for the error module."""

from pysmartthings.errors import APIResponseError


class TestAPIResponseError:
"""Tests for the APIResponseError class."""

@staticmethod
def test_is_target_error():
"""Tests the initialization."""
# Arrange/Act
data = {
"requestId": "8B66A345-03B0-477F-A8A6-1A1CF0277040",
"error": {
"code": "ConstraintViolationError",
"message": "The request is malformed.",
"details": [
{
"code": "TargetTimeoutError",
"target": "https://blah.blah/blah",
"message": "Upstream target timed out",
"details": []
}
]
}
}
error = APIResponseError(None, None, data=data)
# Assert
assert error.is_target_error()
assert error.raw_error_response == data
assert error.request_id == '8B66A345-03B0-477F-A8A6-1A1CF0277040'
assert error.error.code == 'ConstraintViolationError'
assert error.error.message == 'The request is malformed.'
assert len(error.error.details) == 1
detail = error.error.details[0]
assert detail.code == 'TargetTimeoutError'
assert detail.target == "https://blah.blah/blah"
assert detail.message == "Upstream target timed out"
assert not detail.details

0 comments on commit 071e978

Please sign in to comment.