1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-21 22:51:41 +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])
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"),
}

View File

@ -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: