Skip to content

Commit

Permalink
Add introspection of the errors that make up NotConnected (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl authored and ZohebShaikh committed Nov 14, 2024
1 parent bc153ea commit 3a12838
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ophyd_async/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ def __init__(self, errors: ErrorText):

self._errors = errors

@property
def sub_errors(self) -> Mapping[str, Exception]:
if isinstance(self._errors, dict):
return self._errors.copy()
else:
return {}

def _format_sub_errors(self, name: str, error: Exception, indent="") -> str:
if isinstance(error, NotConnected):
error_txt = ":" + error.format_error_string(indent + self._indent_width)
Expand Down
8 changes: 8 additions & 0 deletions tests/core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ async def test_error_handling_device_collector_mock():
assert str(expected_output) == str(e.value)


def test_introspecting_sub_errors():
sub_error1 = NotConnected("bad")
assert sub_error1.sub_errors == {}
sub_error2 = ValueError("very bad")
error = NotConnected({"child1": sub_error1, "child2": sub_error2})
assert error.sub_errors == {"child1": sub_error1, "child2": sub_error2}


async def test_error_handling_device_collector(caplog):
caplog.set_level(10)
with pytest.raises(NotConnected) as e:
Expand Down

0 comments on commit 3a12838

Please sign in to comment.