diff --git a/tangostationcontrol/tangostationcontrol/clients/attribute_wrapper.py b/tangostationcontrol/tangostationcontrol/clients/attribute_wrapper.py index cc0b35730427440bec8b34c2afde756b1b461557..30432ae3fdedb19d2e8b19743e63320906b7908e 100644 --- a/tangostationcontrol/tangostationcontrol/clients/attribute_wrapper.py +++ b/tangostationcontrol/tangostationcontrol/clients/attribute_wrapper.py @@ -145,13 +145,38 @@ class attribute_wrapper(attribute): return value + def _decorate_read_function(self, read_attr_func): + """ Wrap an attribute read function to annotate its exceptions with our + comms_annotation to be able to identify which attribute triggered the error. """ + def wrapper(): + try: + return read_attr_func() + except Exception as e: + raise Exception(f"Failed to read attribute {self.comms_annotation}") from e + + return wrapper + + def _decorate_write_function(self, write_attr_func): + """ Wrap an attribute write function to annotate its exceptions with our + comms_annotation to be able to identify which attribute triggered the error. """ + def wrapper(value): + try: + write_attr_func(value) + except Exception as e: + raise Exception(f"Failed to write attribute {self.comms_annotation}") from e + + return wrapper + def set_comm_client(self, client): """ takes a communications client as input arguments This client should be of a class containing a "get_mapping" function and return a read and write function that the wrapper will use to get/set data. """ try: - self.read_function, self.write_function = client.setup_attribute(self.comms_annotation, self) + read_attr_func, write_attr_func = client.setup_attribute(self.comms_annotation, self) + + self.read_function = self._decorate_read_function(read_attr_func) + self.write_function = self._decorate_write_function(write_attr_func) except Exception as e: raise Exception(f"Exception while setting {client.__class__.__name__} attribute with annotation: '{self.comms_annotation}'") from e @@ -160,7 +185,10 @@ class attribute_wrapper(attribute): Asynchronous version of set_comm_client. """ try: - self.read_function, self.write_function = await client.setup_attribute(self.comms_annotation, self) + read_attr_func, write_attr_func = await client.setup_attribute(self.comms_annotation, self) + + self.read_function = self._decorate_read_function(read_attr_func) + self.write_function = self._decorate_write_function(write_attr_func) except Exception as e: raise Exception(f"Exception while setting {client.__class__.__name__} attribute with annotation: '{self.comms_annotation}'") from e