1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-22 07:00:12 +01:00

Create a SDKException for DNS errors

This commit is contained in:
Felipe Martins Diel 2020-09-15 22:55:37 -03:00 committed by Matthew Garrett
parent 5af3a81264
commit 20d1d63fc3
2 changed files with 16 additions and 11 deletions

View File

@ -13,23 +13,19 @@ class BroadlinkException(Exception):
self.strerror = "%s: %s" % (args[1], args[2]) self.strerror = "%s: %s" % (args[1], args[2])
elif len(args) == 2: elif len(args) == 2:
self.errno = args[0] self.errno = args[0]
self.strerror = args[1] self.strerror = str(args[1])
elif len(args) == 1: elif len(args) == 1:
self.errno = None self.errno = None
self.strerror = args[0] self.strerror = str(args[0])
else: else:
self.errno = None self.errno = None
self.strerror = None self.strerror = ""
def __str__(self): def __str__(self):
"""Return the error message.""" """Return the error message."""
if self.errno is not None: if self.errno is not None:
err_msg = "[Errno %s] %s" % (self.errno, self.strerror) return "[Errno %s] %s" % (self.errno, self.strerror)
elif self.strerror is not None: return self.strerror
err_msg = "%s" % (self.strerror)
else:
err_msg = ""
return err_msg
class FirmwareException(BroadlinkException): class FirmwareException(BroadlinkException):
@ -122,6 +118,12 @@ class LengthError(SDKException):
pass pass
class DNSError(SDKException):
"""Domain name resolution error."""
pass
class NetworkTimeoutError(SDKException): class NetworkTimeoutError(SDKException):
"""Network timeout error.""" """Network timeout error."""
@ -151,6 +153,7 @@ BROADLINK_EXCEPTIONS = {
-4000: (NetworkTimeoutError, "Network timeout"), -4000: (NetworkTimeoutError, "Network timeout"),
-4007: (LengthError, "Received data packet length error"), -4007: (LengthError, "Received data packet length error"),
-4008: (ChecksumError, "Received data packet check error"), -4008: (ChecksumError, "Received data packet check error"),
-4013: (DNSError, "Domain name resolution error"),
} }

View File

@ -1,6 +1,8 @@
"""Helper functions.""" """Helper functions."""
import socket import socket
from .exceptions import exception
def get_local_ip() -> str: def get_local_ip() -> str:
"""Try to determine the local IP address of the machine.""" """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()) local_ip_address = socket.gethostbyname(socket.gethostname())
if not local_ip_address.startswith('127.'): if not local_ip_address.startswith('127.'):
return local_ip_address return local_ip_address
except OSError: except socket.gaierror:
pass raise exception(-4013) # DNS Error
# Connecting to UDP address does not send packets. # Connecting to UDP address does not send packets.
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: