Skip to content

Commit

Permalink
Provide channel information when firing HandlingErrors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed May 2, 2024
1 parent a88a898 commit 78aca66
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions org.jgrapes.core/src/org/jgrapes/core/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ public Optional<EventPipeline> processedBy() {
@Override
protected void handlingError(
EventPipeline eventProcessor, Throwable throwable) {
eventProcessor.fire(
new HandlingError(this, throwable), channels());
if (invokedFor != null) {
eventProcessor.fire(new HandlingError(this, throwable), invokedFor);
} else {
eventProcessor.fire(new HandlingError(this, throwable), channels());
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions org.jgrapes.core/src/org/jgrapes/core/internal/EventBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public abstract class EventBase<T>
* {@link Event#completionEvents()}, which wraps the result in
* an unmodifiable set. */
protected Set<Event<?>> completionEvents;
/** Temporarily set when invoking a handler, only to be used by
* {@link #handlingError(EventPipeline, Throwable)}. */
protected Channel invokedFor;
/** Set when the event has been completed. */
protected boolean completed;
private boolean requiresResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ private void invokeHandlers(Iterator<HandlerReference> handlers,
// JUnit support
CoreUtils.setAssertionError(t);
event.handlingError(asEventPipeline, t);
event.invokedFor = null;
} catch (Error e) { // NOPMD
// Wouldn't have caught it, if it was possible.
throw e;
} catch (Throwable t) { // NOPMD
// Errors have been rethrown, so this should work.
event.handlingError(asEventPipeline, t);
event.invokedFor = null;
}
}
} catch (AssertionError t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public void invoke(EventBase<?> event) throws Throwable {
Class<?> channelParam = method.type().parameterType(1);
for (Channel channel : event.channels()) {
if (channelParam.isAssignableFrom(channel.getClass())) {
event.invokedFor = channel;
method.invoke(event, channel);
event.invokedFor = null;
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ public void invoke(EventBase<?> event) throws Throwable {
for (Channel channel : event.channels()) {
if (channelParam.isAssignableFrom(channel.getClass())) {
handlerFound = true;
event.invokedFor = channel;
invocation = reportInvocation(event, false);
method.invoke(event, channel);
reportResult(event, invocation);
event.invokedFor = null;
}
}
if (!handlerFound) {
Expand Down

0 comments on commit 78aca66

Please sign in to comment.