Compare commits

...

2 Commits

Author SHA1 Message Date
52719d22d4
Merge pull request 'Transparently Propagate Event Handler Errors' (#14) from b/error-passthrough into develop
Reviewed-on: https://git.kske.dev/kske/event-bus/pulls/14
Reviewed-by: delvh <leon@kske.dev>
Reviewed-by: DieGurke <maxi@kske.dev>
2021-03-16 08:17:41 +01:00
122106bf39
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.
2021-03-15 08:29:15 +01:00

View File

@ -67,7 +67,7 @@ public final class EventBus {
* @throws EventBusException if an event handler isn't accessible or has an invalid signature * @throws EventBusException if an event handler isn't accessible or has an invalid signature
* @since 0.0.1 * @since 0.0.1
*/ */
public void dispatch(Object event) { public void dispatch(Object event) throws EventBusException {
Objects.requireNonNull(event); Objects.requireNonNull(event);
logger.log(Level.INFO, "Dispatching event {0}", event); logger.log(Level.INFO, "Dispatching event {0}", event);
@ -90,6 +90,10 @@ public final class EventBus {
// Warn about system event not being handled // Warn about system event not being handled
logger.log(Level.WARNING, event + " not handled due to exception", e); 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 else
// Dispatch exception event // Dispatch exception event