From de41883317c9992be2e97fb992753638cca546b9 Mon Sep 17 00:00:00 2001 From: Jan David Mol <mol@astron.nl> Date: Mon, 20 Dec 2021 11:35:45 +0100 Subject: [PATCH] L2SS-548: Annotate which attribute is giving errors --- .../clients/attribute_wrapper.py | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tangostationcontrol/tangostationcontrol/clients/attribute_wrapper.py b/tangostationcontrol/tangostationcontrol/clients/attribute_wrapper.py index cc0b35730..30432ae3f 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 -- GitLab