-
-
Notifications
You must be signed in to change notification settings - Fork 48
Handle missing paths in GetDeviceInfo #386
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
Open
mr-miles
wants to merge
3
commits into
iMicknl:main
Choose a base branch
from
mr-miles:talktalk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Logic to spot and create ActionErrorExceptions.""" | ||
|
|
||
| from .const import ( | ||
| XMO_ACCESS_RESTRICTION_ERR, | ||
| XMO_AUTHENTICATION_ERR, | ||
| XMO_LOGIN_RETRY_ERR, | ||
| XMO_MAX_SESSION_COUNT_ERR, | ||
| XMO_NO_ERR, | ||
| XMO_NON_WRITABLE_PARAMETER_ERR, | ||
| XMO_REQUEST_ACTION_ERR, | ||
| XMO_UNKNOWN_PATH_ERR, | ||
| ) | ||
| from .exceptions import ( | ||
| AccessRestrictionException, | ||
| AuthenticationException, | ||
| LoginRetryErrorException, | ||
| MaximumSessionCountException, | ||
| NonWritableParameterException, | ||
| UnknownException, | ||
| UnknownPathException, | ||
| ) | ||
|
|
||
|
|
||
| class ActionErrorHandler: | ||
| """Raised when a requested action has an error.""" | ||
|
|
||
| KNOWN_EXCEPTIONS = ( | ||
| XMO_AUTHENTICATION_ERR, | ||
| XMO_ACCESS_RESTRICTION_ERR, | ||
| XMO_NON_WRITABLE_PARAMETER_ERR, | ||
| XMO_UNKNOWN_PATH_ERR, | ||
| XMO_MAX_SESSION_COUNT_ERR, | ||
| XMO_LOGIN_RETRY_ERR, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def throw_if_error(response, ignore_unknown_path: bool = False) -> None: | ||
| """Raise the first action-level error, or do nothing if all actions succeeded. | ||
|
|
||
| :param ignore_unknown_path: if True, silently ignore UnknownPathException | ||
| """ | ||
| if response["reply"]["error"]["description"] != XMO_REQUEST_ACTION_ERR: | ||
| return | ||
|
|
||
| for action in response["reply"]["actions"]: | ||
| action_error = action["error"] | ||
| action_error_desc = action_error["description"] | ||
| if action_error_desc != XMO_NO_ERR: | ||
| exc = ActionErrorHandler.from_error_description(action_error, action_error_desc) | ||
| if ignore_unknown_path and isinstance(exc, UnknownPathException): | ||
| continue | ||
| raise exc | ||
|
|
||
| @staticmethod | ||
| def throw_if_error_at(response, index: int, ignore_unknown_path: bool = False) -> None: | ||
| """Raise the error for a specific action, or do nothing if it succeeded. | ||
|
|
||
| :param ignore_unknown_path: if True, silently ignore UnknownPathException | ||
| """ | ||
| try: | ||
| action_error = response["reply"]["actions"][index]["error"] | ||
| except (KeyError, IndexError): | ||
| return | ||
|
|
||
| action_error_desc = action_error["description"] | ||
| if action_error_desc == XMO_NO_ERR: | ||
| return | ||
|
|
||
| exc = ActionErrorHandler.from_error_description(action_error, action_error_desc) | ||
| if ignore_unknown_path and isinstance(exc, UnknownPathException): | ||
| return | ||
| raise exc | ||
|
|
||
| @staticmethod | ||
| def from_error_description(action_error, action_error_desc): | ||
| """Create the correct exception from an error, for the caller to throw.""" | ||
| # pylint: disable=too-many-return-statements | ||
|
|
||
| if action_error_desc == XMO_AUTHENTICATION_ERR: | ||
| return AuthenticationException(action_error) | ||
|
|
||
| if action_error_desc == XMO_ACCESS_RESTRICTION_ERR: | ||
| return AccessRestrictionException(action_error) | ||
|
|
||
| if action_error_desc == XMO_NON_WRITABLE_PARAMETER_ERR: | ||
| return NonWritableParameterException(action_error) | ||
|
|
||
| if action_error_desc == XMO_UNKNOWN_PATH_ERR: | ||
| return UnknownPathException(action_error) | ||
|
|
||
| if action_error_desc == XMO_MAX_SESSION_COUNT_ERR: | ||
| return MaximumSessionCountException(action_error) | ||
|
|
||
| if action_error_desc == XMO_LOGIN_RETRY_ERR: | ||
| return LoginRetryErrorException(action_error) | ||
|
|
||
| return UnknownException(action_error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "reply": { | ||
| "uid": 0, | ||
| "id": 1, | ||
| "error": { | ||
| "code": 16777236, | ||
| "description": "XMO_REQUEST_ACTION_ERR" | ||
| }, | ||
| "actions": [ | ||
| { | ||
| "uid": 1, | ||
| "id": 0, | ||
| "error": { | ||
| "code": 16777221, | ||
| "description": "XMO_UNKNOWN_PATH_ERR" | ||
| }, | ||
| "callbacks": [] | ||
| } | ||
| ], | ||
| "events": [] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| { | ||
| "reply": { | ||
| "uid": 0, | ||
| "id": 1, | ||
| "error": { | ||
| "code": 16777236, | ||
| "description": "XMO_REQUEST_ACTION_ERR" | ||
| }, | ||
| "actions": [ | ||
| { | ||
| "uid": 1, | ||
| "id": 0, | ||
| "error": { | ||
| "code": 16777238, | ||
| "description": "XMO_NO_ERR" | ||
| }, | ||
| "callbacks": [ | ||
| { | ||
| "uid": 1, | ||
| "result": { | ||
| "code": 16777238, | ||
| "description": "XMO_NO_ERR" | ||
| }, | ||
| "xpath": "Device/DeviceInfo/MACAddress", | ||
| "parameters": { | ||
| "value": "AA:BB:CC:DD:EE:FF" | ||
| } | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "uid": 2, | ||
| "id": 1, | ||
| "error": { | ||
| "code": 16777221, | ||
| "description": "XMO_UNKNOWN_PATH_ERR" | ||
| }, | ||
| "callbacks": [] | ||
| } | ||
| ], | ||
| "events": [] | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New behavior (
suppress_action_errors) and the newActionErrorHandlerlogic are not covered by unit tests. Given existing unit tests for action-level authentication errors, add tests that (1) verify unknown-path errors can be suppressed for optional fields, and (2) verify non-unknown-path action errors (e.g., authentication) still raise even when suppression is enabled for other fields.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed: Added 5 new unit tests in
tests/unit/test_client_basic.pycovering:None(single xpath)suppress_action_errors=Falsesuppress_action_errors=True(single xpath)Nonesuppress_action_errors=True(multi xpath)As a bonus improvement, the
throw_if()pattern (raise then catch at call site) was replaced withthrow_if_error()andthrow_if_error_at()methods that raise directly, avoiding the cost of constructing and catching exceptions at every call site.