Skip to content

Commit 3d95c43

Browse files
tpoliawabbiemery
authored andcommitted
Wrap callback exceptions into single group
1 parent 4201ae2 commit 3d95c43

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/blueapi/core/event.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ def publish(self, event: E, correlation_id: str | None = None) -> None:
7878
correlation_id: An optional ID that may be used to correlate this
7979
event with other events
8080
"""
81-
81+
errs = []
8282
for callback in list(self._subscriptions.values()):
8383
try:
8484
callback(event, correlation_id)
8585
except Exception as e:
86-
LOGGER.error(
87-
f"Failed to send event {event} with {correlation_id=}",
88-
exc_info=e,
89-
)
86+
errs.append(e)
87+
if errs:
88+
raise ExceptionGroup(f"Error(s) publishing event: {event}", errs)

tests/unit_tests/core/test_event.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ def test_callback_exceptions_are_contained(publisher: EventPublisher[MyEvent]):
8686
publisher.subscribe(handler)
8787
publisher.subscribe(handler)
8888

89-
publisher.publish(event, c_id)
89+
with pytest.RaisesGroup(ValueError):
90+
publisher.publish(event, c_id)
9091

91-
# Both handlers should be called and the exception should not be raised from publish
92+
# Both handlers should be called but the exception should still be raised
9293
handler.assert_has_calls([mock.call(event, c_id), mock.call(event, c_id)])
9394

9495

0 commit comments

Comments
 (0)