Skip to content

Commit

Permalink
Merge branch 'feature/raise-error' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoblath committed Dec 16, 2024
2 parents 7f6285c + f460b69 commit 7d06fe8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
11 changes: 4 additions & 7 deletions dripline/core/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
except ImportError:
# optional only when doing a docs build
pass

#TODO need to put exceptions back, this is fragile/wrong right now
#from . import exceptions
from .throw_reply import ThrowReply


__all__ = []
Expand Down Expand Up @@ -44,18 +42,17 @@ def wrapper(self, *args, **kwargs):
try:
cal = evaluator(eval_str)
except OverflowError:
logger.debug('GOT AN OVERFLOW ERROR')
cal = None
raise ThrowReply('service_error_bad_payload', 'GOT AN OVERFLOW ERROR')
except Exception as e:
raise exceptions.DriplineValueError(repr(e), result=val_dict)
raise ThrowReply('service_error_invalid_value', repr(e))
if cal is not None:
val_dict['value_cal'] = cal
elif isinstance(self._calibration, dict):
logger.debug('calibration is dictionary, looking up value')
if val_dict['value_raw'] in self._calibration:
val_dict['value_cal'] = self._calibration[val_dict['value_raw']]
else:
raise exceptions.DriplineValueError(f"raw value <{repr(val_dict['value_raw'])}> not in cal dict", result=val_dict)
raise ThrowReply('service_error_invalid_value', f"raw value <{repr(val_dict['value_raw'])}> not in cal dict")
else:
logger.warning('the _calibration property is of unknown type')
return val_dict
Expand Down
6 changes: 3 additions & 3 deletions dripline/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_on_set(self, value):
self.on_set = _log_on_set_decoration(self.on_set)
else:
if self.log_on_set:
raise ValueError("unable to disable get_on_set while log_on_set is enabled")
raise ThrowReply('service_error_invalid_value', 'unable to disable get_on_set while log_on_set is enabled')
self.on_set = self.__initial_on_set
self._get_on_set = bool(value)

Expand All @@ -111,7 +111,7 @@ def log_on_set(self):
def log_on_set(self, value):
if value:
if not self.get_on_set:
raise ValueError("unable to enable log_on_set when get_on_set is disabled")
raise ThrowReply('service_error_invalid_value', 'unable to enable log_on_set when get_on_set is disabled')
self.on_set = _log_on_set_decoration(self, self.on_set)
else:
self.on_set = self.__initial_on_set
Expand All @@ -131,7 +131,7 @@ def log_interval(self, new_interval):
elif isinstance(new_interval, datetime.timedelta):
self._log_interval = new_interval
else:
raise ValueError(f"unable to interpret a new_interval of type <{type(new_interval)}>")
raise ThrowReply('service_error_invalid_value', f"unable to interpret a new_interval of type <{type(new_interval)}>")

def scheduled_log(self):
logger.debug("in a scheduled log event")
Expand Down
11 changes: 5 additions & 6 deletions dripline/implementations/entity_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self,
'''
Entity.__init__(self, **kwargs)
if base_str is None:
raise ValueError('<base_str> is required to __init__ SimpleSCPIEntity instance')
raise ThrowReply('service_error_invalid_value', '<base_str> is required to __init__ SimpleSCPIEntity instance')
else:
self.cmd_base = base_str

Expand Down Expand Up @@ -120,10 +120,10 @@ def __init__(self,
self._extract_raw_regex = extract_raw_regex
self.evaluator = asteval.Interpreter()
if set_value_map is not None and not isinstance(set_value_map, (dict,str)):
raise ValueError(f"Invalid set_value_map config for {self.name}; type is {type(set_value_map)} not dict")
raise ThrowReply('service_error_invalid_value', f"Invalid set_value_map config for {self.name}; type is {type(set_value_map)} not dict")
self._set_value_lowercase = set_value_lowercase
if isinstance(set_value_map, dict) and not set_value_lowercase:
raise ValueError(f"Invalid config option for {self.name} with set_value_map and set_value_lowercase=False")
raise ThrowReply('service_error_invalid_value', f"Invalid config option for {self.name} with set_value_map and set_value_lowercase=False")

@calibrate()
def on_get(self):
Expand All @@ -137,16 +137,15 @@ def on_get(self):
matches = re.search(self._extract_raw_regex, first_result)
if matches is None:
logger.error('matching returned none')
# exceptions.DriplineValueError
raise ThrowReply('resource_error', 'device returned unparsable result, [{}] has no match to input regex [{}]'.format(first_result, self._extract_raw_regex))
raise ThrowReply('service_error_invalid_value', 'device returned unparsable result, [{}] has no match to input regex [{}]'.format(first_result, self._extract_raw_regex))
logger.debug(f"matches are: {matches.groupdict()}")
result = matches.groupdict()['value_raw']
return result

def on_set(self, value):
if self._set_str is None:
# exceptions.DriplineMethodNotSupportedError
raise ThrowReply('service_error', f"endpoint '{self.name}' does not support set")
raise ThrowReply('message_error_invalid_method', f"endpoint '{self.name}' does not support set")
if isinstance(value, str) and self._set_value_lowercase:
value = value.lower()
if self._set_value_map is None:
Expand Down

0 comments on commit 7d06fe8

Please sign in to comment.