From f4dbbe61f89d8d4a1753058c5d69cc71467213d4 Mon Sep 17 00:00:00 2001 From: "Walter C. Pettus" Date: Fri, 6 Dec 2024 17:16:39 -0500 Subject: [PATCH 1/3] Fix raise error convention --- dripline/core/calibrate.py | 6 ++---- dripline/implementations/entity_endpoints.py | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/dripline/core/calibrate.py b/dripline/core/calibrate.py index f0da80b8..2f9254c2 100644 --- a/dripline/core/calibrate.py +++ b/dripline/core/calibrate.py @@ -4,8 +4,6 @@ # optional only when doing a docs build pass -#TODO need to put exceptions back, this is fragile/wrong right now -#from . import exceptions __all__ = [] @@ -47,7 +45,7 @@ def wrapper(self, *args, **kwargs): logger.debug('GOT AN OVERFLOW ERROR') cal = None except Exception as e: - raise exceptions.DriplineValueError(repr(e), result=val_dict) + raise ValueError(repr(e)) if cal is not None: val_dict['value_cal'] = cal elif isinstance(self._calibration, dict): @@ -55,7 +53,7 @@ def wrapper(self, *args, **kwargs): 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 ValueError(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 diff --git a/dripline/implementations/entity_endpoints.py b/dripline/implementations/entity_endpoints.py index d6b4ddcc..292798d7 100644 --- a/dripline/implementations/entity_endpoints.py +++ b/dripline/implementations/entity_endpoints.py @@ -137,8 +137,7 @@ 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 ValueError('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 @@ -146,7 +145,7 @@ def on_get(self): 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: From 9896924d617f2119ed8cffa6fb43ad7cb57b8fb1 Mon Sep 17 00:00:00 2001 From: Noah Oblath Date: Fri, 6 Dec 2024 15:32:15 -0800 Subject: [PATCH 2/3] Closing quotes --- dripline/core/calibrate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dripline/core/calibrate.py b/dripline/core/calibrate.py index 2f9254c2..fdaf01f8 100644 --- a/dripline/core/calibrate.py +++ b/dripline/core/calibrate.py @@ -53,7 +53,7 @@ def wrapper(self, *args, **kwargs): if val_dict['value_raw'] in self._calibration: val_dict['value_cal'] = self._calibration[val_dict['value_raw']] else: - raise ValueError(f"raw value <{repr(val_dict['value_raw'])}> not in cal dict) + raise ValueError(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 From f460b69a24b1361f47081654824b8ae6c3f78096 Mon Sep 17 00:00:00 2001 From: "Walter C. Pettus" Date: Fri, 13 Dec 2024 22:58:41 -0500 Subject: [PATCH 3/3] convert errors to dripline ThrowReply --- dripline/core/calibrate.py | 9 ++++----- dripline/core/entity.py | 6 +++--- dripline/implementations/entity_endpoints.py | 8 ++++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/dripline/core/calibrate.py b/dripline/core/calibrate.py index fdaf01f8..f9c50ab8 100644 --- a/dripline/core/calibrate.py +++ b/dripline/core/calibrate.py @@ -3,7 +3,7 @@ except ImportError: # optional only when doing a docs build pass - +from .throw_reply import ThrowReply __all__ = [] @@ -42,10 +42,9 @@ 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 ValueError(repr(e)) + raise ThrowReply('service_error_invalid_value', repr(e)) if cal is not None: val_dict['value_cal'] = cal elif isinstance(self._calibration, dict): @@ -53,7 +52,7 @@ def wrapper(self, *args, **kwargs): if val_dict['value_raw'] in self._calibration: val_dict['value_cal'] = self._calibration[val_dict['value_raw']] else: - raise ValueError(f"raw value <{repr(val_dict['value_raw'])}> not in cal 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 diff --git a/dripline/core/entity.py b/dripline/core/entity.py index d89aecb0..cb9ef3bd 100644 --- a/dripline/core/entity.py +++ b/dripline/core/entity.py @@ -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) @@ -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 @@ -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") diff --git a/dripline/implementations/entity_endpoints.py b/dripline/implementations/entity_endpoints.py index 292798d7..fb3d21de 100644 --- a/dripline/implementations/entity_endpoints.py +++ b/dripline/implementations/entity_endpoints.py @@ -39,7 +39,7 @@ def __init__(self, ''' Entity.__init__(self, **kwargs) if base_str is None: - raise ValueError(' is required to __init__ SimpleSCPIEntity instance') + raise ThrowReply('service_error_invalid_value', ' is required to __init__ SimpleSCPIEntity instance') else: self.cmd_base = base_str @@ -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): @@ -137,7 +137,7 @@ def on_get(self): matches = re.search(self._extract_raw_regex, first_result) if matches is None: logger.error('matching returned none') - raise ValueError('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