Skip to content

Commit 22d493e

Browse files
WasabiFandwalton76
authored andcommitted
Throw friendly errors (ev3dev#342)
* Untested implementation of friendly errors * Fix syntax * Use specific exception type * Fix and test error implementation
1 parent b8d780e commit 22d493e

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

‎ev3dev/core.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import shlex
5050
import stat
5151
import time
52+
import errno
5253
from os.path import abspath
5354
from struct import pack, unpack
5455
from subprocess import Popen, check_output, PIPE
@@ -204,12 +205,31 @@ def _set_attribute(self, attribute, name, value):
204205
attribute = self._attribute_file_open( name )
205206
else:
206207
attribute.seek(0)
207-
attribute.write(value.encode())
208-
attribute.flush()
208+
209+
try:
210+
attribute.write(value.encode())
211+
attribute.flush()
212+
except Exception as ex:
213+
self._raise_friendly_access_error(ex, name)
209214
return attribute
210215
else:
211216
raise Exception('Device is not connected')
212217

218+
def _raise_friendly_access_error(self, driver_error, attribute):
219+
if not isinstance(driver_error, OSError):
220+
raise driver_error
221+
222+
if driver_error.errno == errno.EINVAL:
223+
if attribute == "speed_sp":
224+
try:
225+
max_speed = self.max_speed
226+
except (AttributeError, Exception):
227+
raise ValueError("The given speed value was out of range") from driver_error
228+
else:
229+
raise ValueError("The given speed value was out of range. Max speed: +/-" + str(max_speed)) from driver_error
230+
raise ValueError("One or more arguments were out of range or invalid") from driver_error
231+
raise driver_error
232+
213233
def get_attr_int(self, attribute, name):
214234
attribute, value = self._get_attribute(attribute, name)
215235
return attribute, int(value)

0 commit comments

Comments
 (0)