Skip to content
Snippets Groups Projects
Commit fa162a94 authored by Corné Lukken's avatar Corné Lukken
Browse files

Merge branch 'L2SS-548-log-which-attribute-errors' into 'master'

L2SS-548: Annotate which attribute is giving errors

Closes L2SS-548

See merge request !201
parents 984678a8 de418833
No related branches found
No related tags found
1 merge request!201L2SS-548: Annotate which attribute is giving errors
...@@ -145,13 +145,38 @@ class attribute_wrapper(attribute): ...@@ -145,13 +145,38 @@ class attribute_wrapper(attribute):
return value 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): 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 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. and return a read and write function that the wrapper will use to get/set data.
""" """
try: 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: except Exception as e:
raise Exception(f"Exception while setting {client.__class__.__name__} attribute with annotation: '{self.comms_annotation}'") from 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): ...@@ -160,7 +185,10 @@ class attribute_wrapper(attribute):
Asynchronous version of set_comm_client. Asynchronous version of set_comm_client.
""" """
try: 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: except Exception as e:
raise Exception(f"Exception while setting {client.__class__.__name__} attribute with annotation: '{self.comms_annotation}'") from e raise Exception(f"Exception while setting {client.__class__.__name__} attribute with annotation: '{self.comms_annotation}'") from e
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment