Skip to content

Commit 3171043

Browse files
WasabiFandwalton76
authored andcommitted
Throw friendly error when device is disconnected (#373)
* Throw friendly error when device is disconnected * Update error code
1 parent 1350cd5 commit 3171043

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

ev3dev/core.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def _attribute_file_open( self, name ):
223223
def _get_attribute(self, attribute, name):
224224
"""Device attribute getter"""
225225
if self.connected:
226-
if None == attribute:
226+
if attribute is None:
227227
attribute = self._attribute_file_open( name )
228228
else:
229229
attribute.seek(0)
@@ -235,12 +235,12 @@ def _get_attribute(self, attribute, name):
235235
def _set_attribute(self, attribute, name, value):
236236
"""Device attribute setter"""
237237
if self.connected:
238-
if None == attribute:
239-
attribute = self._attribute_file_open( name )
240-
else:
241-
attribute.seek(0)
242-
243238
try:
239+
if attribute is None:
240+
attribute = self._attribute_file_open( name )
241+
else:
242+
attribute.seek(0)
243+
244244
attribute.write(value.encode())
245245
attribute.flush()
246246
except Exception as ex:
@@ -263,6 +263,11 @@ def _raise_friendly_access_error(self, driver_error, attribute):
263263
else:
264264
raise ValueError("The given speed value was out of range. Max speed: +/-" + str(max_speed)) from driver_error
265265
raise ValueError("One or more arguments were out of range or invalid") from driver_error
266+
elif driver_error.errno == errno.ENODEV or driver_error.errno == errno.ENOENT:
267+
# We will assume that a file-not-found error is the result of a disconnected device
268+
# rather than a library error. If that isn't the case, at a minimum the underlying
269+
# error info will be printed for debugging.
270+
raise Exception("%s is no longer connected" % self) from driver_error
266271
raise driver_error
267272

268273
def get_attr_int(self, attribute, name):

0 commit comments

Comments
 (0)