Skip to content

Commit

Permalink
p/device: Expose underlying error on DeviceError
Browse files Browse the repository at this point in the history
  • Loading branch information
rockstorm101 committed Oct 13, 2023
1 parent f3ccaf5 commit a668139
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions printrun/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Device():
parity_workaround : bool, optional
On serial connections, enable/disable a workaround on parity
checking. Not all platforms need to do this parity workaround, and
some drivers don't support it. By default it is disable.
some drivers don't support it. By default it is disabled.
Attributes
----------
Expand Down Expand Up @@ -110,7 +110,7 @@ def connect(self, port=None, baudrate=None):
self._parse_type()
getattr(self, "_connect_" + self._type)()
else:
raise DeviceError("No port or URL specified.")
raise DeviceError("No port or URL specified")

def disconnect(self):
"""Terminates the connection to the device."""
Expand Down Expand Up @@ -160,7 +160,7 @@ def readline(self) -> bytes:
# TODO: silent fail on no device? return timeout?
if self._device is not None:
return getattr(self, "_readline_" + self._type)()
raise DeviceError("Attempting to read when disconnected")
raise DeviceError("Attempted to read when disconnected")

def reset(self):
"""Attempt to reset the connection to the device.
Expand Down Expand Up @@ -195,7 +195,7 @@ def write(self, data: bytes):
if self._device is not None:
getattr(self, "_write_" + self._type)(data)
else:
raise DeviceError("Attempting to write when disconnected")
raise DeviceError("Attempted to write when disconnected")

def _parse_type(self):
# Guess which type of connection is being used
Expand Down Expand Up @@ -251,9 +251,8 @@ def _connect_serial(self):
self._device.open()

except (serial.SerialException, IOError) as e:
msg = (f"Could not connect to serial port {self.port} "
f"at baudrate {self.baudrate}\n{e}")
raise DeviceError(msg) from e
msg = "Could not connect to serial port '{}'".format(self.port)
raise DeviceError(msg, e) from e

def _is_connected_serial(self):
return self._device.is_open
Expand All @@ -262,16 +261,16 @@ def _disconnect_serial(self):
try:
self._device.close()
except serial.SerialException as e:
msg = "Error at disconnecting."
raise DeviceError(msg) from e
msg = "Error on serial disconnection"
raise DeviceError(msg, e) from e

def _readline_serial(self):
try:
# Serial.readline() returns b'' (aka `READ_EMPTY`) on timeout
return self._device.readline()
except (serial.SerialException, OSError) as e:
msg = f"Unable to read from serial port '{self.port}'"
raise DeviceError(msg) from e
raise DeviceError(msg, e) from e

def _reset_serial(self):
self._device.dtr = True
Expand All @@ -282,8 +281,8 @@ def _write_serial(self, data):
try:
self._device.write(data)
except serial.SerialException as e:
msg = "Unable to write to serial port."
raise DeviceError(msg) from e
msg = "Unable to write to serial port '{self.port}'"
raise DeviceError(msg, e) from e

def _disable_ttyhup(self):
if platform.system() == "Linux":
Expand All @@ -310,9 +309,9 @@ def _connect_socket(self):

except OSError as e:
self._disconnect_socket()
msg = (f"Could not connect to "
f"'{self._hostname}:{self._port_number}'.\n{e}")
raise DeviceError(msg) from e
msg = "Could not connect to {}:{}".format(self._hostname,
self._port_number)
raise DeviceError(msg, e) from e

def _is_connected_socket(self):
# TODO: current implementation tracks status of connection but
Expand All @@ -330,8 +329,8 @@ def _disconnect_socket(self):
self._selector = None
self._device.close()
except OSError as e:
msg = "Error at disconnecting."
raise DeviceError(msg) from e
msg = "Error on socket disconnection"
raise DeviceError(msg, e) from e

def _readline_socket(self):
SYS_AGAIN = None # python's marker for timeout/no data
Expand Down Expand Up @@ -362,8 +361,9 @@ def _readline_socket(self):
return READ_EOF
except OSError as e:
self._is_connected = False
msg = "Unable to read from socket. Connection lost."
raise DeviceError(msg) from e
msg = ("Unable to read from {}:{}. Connection lost"
).format(self._hostname, self._port_number)
raise DeviceError(msg, e) from e

def _readline_buf(self):
# Try to readline from buffer
Expand All @@ -387,15 +387,30 @@ def _write_socket(self, data):
pass
except (OSError, RuntimeError) as e:
self._is_connected = False
msg = "Unable to write to socket. Connection lost."
raise DeviceError(msg) from e
msg = ("Unable to write to {}:{}. Connection lost"
).format(self._hostname, self._port_number)
raise DeviceError(msg, e) from e


class DeviceError(Exception):
"""Raised on any connection error.
One exception groups all connection errors regardless of the underlying
connection or error type. Error trace will provide additional information
related to the relevant connection type.
connection or error type.
Parameters
----------
msg : str
Error message.
cause : Exception, optional
Underlying error.
Attributes
----------
cause
"""

def __init__(self, msg, cause=None):
super().__init__(msg)
self.cause = cause

0 comments on commit a668139

Please sign in to comment.