From 20d1d63fc3b8a96da517d52a58d7497526fdc11b Mon Sep 17 00:00:00 2001 From: Felipe Martins Diel Date: Tue, 15 Sep 2020 22:55:37 -0300 Subject: [PATCH] Create a SDKException for DNS errors --- broadlink/exceptions.py | 21 ++++++++++++--------- broadlink/helpers.py | 6 ++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/broadlink/exceptions.py b/broadlink/exceptions.py index dfa0df9..0b73e55 100644 --- a/broadlink/exceptions.py +++ b/broadlink/exceptions.py @@ -13,23 +13,19 @@ class BroadlinkException(Exception): self.strerror = "%s: %s" % (args[1], args[2]) elif len(args) == 2: self.errno = args[0] - self.strerror = args[1] + self.strerror = str(args[1]) elif len(args) == 1: self.errno = None - self.strerror = args[0] + self.strerror = str(args[0]) else: self.errno = None - self.strerror = None + self.strerror = "" def __str__(self): """Return the error message.""" if self.errno is not None: - err_msg = "[Errno %s] %s" % (self.errno, self.strerror) - elif self.strerror is not None: - err_msg = "%s" % (self.strerror) - else: - err_msg = "" - return err_msg + return "[Errno %s] %s" % (self.errno, self.strerror) + return self.strerror class FirmwareException(BroadlinkException): @@ -122,6 +118,12 @@ class LengthError(SDKException): pass +class DNSError(SDKException): + """Domain name resolution error.""" + + pass + + class NetworkTimeoutError(SDKException): """Network timeout error.""" @@ -151,6 +153,7 @@ BROADLINK_EXCEPTIONS = { -4000: (NetworkTimeoutError, "Network timeout"), -4007: (LengthError, "Received data packet length error"), -4008: (ChecksumError, "Received data packet check error"), + -4013: (DNSError, "Domain name resolution error"), } diff --git a/broadlink/helpers.py b/broadlink/helpers.py index af0c8c6..40f06e3 100644 --- a/broadlink/helpers.py +++ b/broadlink/helpers.py @@ -1,6 +1,8 @@ """Helper functions.""" import socket +from .exceptions import exception + def get_local_ip() -> str: """Try to determine the local IP address of the machine.""" @@ -9,8 +11,8 @@ def get_local_ip() -> str: local_ip_address = socket.gethostbyname(socket.gethostname()) if not local_ip_address.startswith('127.'): return local_ip_address - except OSError: - pass + except socket.gaierror: + raise exception(-4013) # DNS Error # Connecting to UDP address does not send packets. with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: