From 5dafc1d1ed8916057bdfcfe6c1ab4971e7a6765d Mon Sep 17 00:00:00 2001 From: Teemu R Date: Tue, 2 Jan 2024 16:24:09 +0100 Subject: [PATCH] Cleanup custom exception kwarg handling (#602) * Cleanup custom exceptions * Read custom keyword arguments from kwargs * Pass all input args to the super Earlier behavior: Got error: AuthenticationException((('Error logging in: 192.168.xx.xx: LOGIN_ERROR(-1501)',), )) New behavior: Got error: AuthenticationException('Error logging in: 192.168.xx.xx: LOGIN_ERROR(-1501)') * Pass UnsupportedDeviceException kwargs to parent, too --- kasa/exceptions.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/kasa/exceptions.py b/kasa/exceptions.py index ff91a7b0..97fabb04 100644 --- a/kasa/exceptions.py +++ b/kasa/exceptions.py @@ -6,39 +6,29 @@ 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) + def __init__(self, *args, **kwargs): + self.error_code: Optional["SmartErrorCode"] = kwargs.get("error_code", None) + super().__init__(*args) class UnsupportedDeviceException(SmartDeviceException): """Exception for trying to connect to unsupported devices.""" - def __init__(self, *args, discovery_result=None): - self.discovery_result = discovery_result - super().__init__(args) + def __init__(self, *args, **kwargs): + self.discovery_result = kwargs.get("discovery_result") + super().__init__(*args, **kwargs) 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."""