8000 Add optional error code to exceptions by sdb9696 · Pull Request #585 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Add optional error code to exceptions #585

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

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions kasa/aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ def _handle_response_error_code(self, resp_dict: dict, msg: str):
return
msg = f"{msg}: {self._host}: {error_code.name}({error_code.value})"
if error_code in SMART_TIMEOUT_ERRORS:
raise TimeoutException(msg)
raise TimeoutException(msg, error_code=error_code)
if error_code in SMART_RETRYABLE_ERRORS:
raise RetryableException(msg)
raise RetryableException(msg, error_code=error_code)
if error_code in SMART_AUTHENTICATION_ERRORS:
self._handshake_done = False
self._login_token = None
raise AuthenticationException(msg)
raise SmartDeviceException(msg)
raise AuthenticationException(msg, error_code=error_code)
raise SmartDeviceException(msg, error_code=error_code)

async def send_secure_passthrough(self, request: str):
"""Send encrypted message as passthrough."""
Expand Down
14 changes: 14 additions & 0 deletions kasa/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""python-kasa exceptions."""
from enum import IntEnum
from typing import Optional


class SmartDeviceException(Exception):
"""Base exception for device errors."""

def __init__(self, *args, error_code: Optional["SmartErrorCode"] = None):
self.error_code = error_code
super().__init__(args)


class UnsupportedDeviceException(SmartDeviceException):
"""Exception for trying to connect to unsupported devices."""
Expand All @@ -17,14 +22,23 @@ def __init__(self, *args, discovery_result=None):
class AuthenticationException(SmartDeviceException):
"""Base exception for device authentication errors."""

def __init__(self, *args, error_code: Optional["SmartErrorCode"] = None):
super().__init__(args, error_code)


class RetryableException(SmartDeviceException):
"""Retryable exception for device errors."""

def __init__(self, *args, error_code: Optional["SmartErrorCode"] = None):
super().__init__(args, error_code)


class TimeoutException(SmartDeviceException):
"""Timeout exception for device errors."""

def __init__(self, *args, error_code: Optional["SmartErrorCode"] = None):
super().__init__(args, error_code)


class SmartErrorCode(IntEnum):
"""Enum for SMART Error Codes."""
Expand Down
8 changes: 4 additions & 4 deletions kasa/smartprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ def _handle_response_error_code(self, resp_dict: dict):
if method := resp_dict.get("method"):
msg += f" for method: {method}"
if error_code in SMART_TIMEOUT_ERRORS:
raise TimeoutException(msg)
raise TimeoutException(msg, error_code=error_code)
if error_code in SMART_RETRYABLE_ERRORS:
raise RetryableException(msg)
raise RetryableException(msg, error_code=error_code)
if error_code in SMART_AUTHENTICATION_ERRORS:
raise AuthenticationException(msg)
raise SmartDeviceException(msg)
raise AuthenticationException(msg, error_code=error_code)
raise SmartDeviceException(msg, error_code=error_code)

async def close(self) -> None:
"""Close the protocol."""
Expand Down
0