From 122106bf39de2895c29f00387c78a553fa6debec Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 15 Mar 2021 08:29:15 +0100 Subject: [PATCH] Transparently propagate event handler errors When an exception occurs during the execution of an event handler, it is caught, wrapped inside an exception event and dispatched on the event bus. This applies to any throwable, but is not very useful for errors, as these are not normally caught. Assertion errors in particular, which are used in unit tests, should not be caught, as this would cause the test runner to miss a failed test. Therefore, errors are now transparently passed through to the caller of the dispatch method. --- .../src/main/java/dev/kske/eventbus/core/EventBus.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java b/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java index a719135..95a65a5 100644 --- a/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java +++ b/event-bus-core/src/main/java/dev/kske/eventbus/core/EventBus.java @@ -67,7 +67,7 @@ public final class EventBus { * @throws EventBusException if an event handler isn't accessible or has an invalid signature * @since 0.0.1 */ - public void dispatch(Object event) { + public void dispatch(Object event) throws EventBusException { Objects.requireNonNull(event); logger.log(Level.INFO, "Dispatching event {0}", event); @@ -90,6 +90,10 @@ public final class EventBus { // Warn about system event not being handled logger.log(Level.WARNING, event + " not handled due to exception", e); + else if (e.getCause() instanceof Error) + + // Transparently pass error to the caller + throw (Error) e.getCause(); else // Dispatch exception event