Search code examples
javakotlinerror-handlingaxon

How can I get the event handler that produced an error in a Axon Error handler?


I just implemented a dead letter for errors handled in ListenerInvocationErrorHandler:

override fun onError(exception: Exception, event: EventMessage<*>, eventHandler: EventMessageHandler) {
        // Insert in dead letter
    }

This works ok, as I've got the information I need (exception, event and event handler), so I can reprocess by just invoking eventHandler.process(event).

However, for errors handled in ErrorHandler (normally persistence errors) I am missing event handler info:

override fun handleError(errorContext: ErrorContext) {
        // No event handler info
    }

Taking into account that each Event handler is isolated in a different Processing group, so each Event handler should run in its own transaction... Is there any way to obtain the event handler that produced the error?


Solution

  • At the ErrorHandler level you are "missing" the Event Handler info, as invoking the ErrorHandler from within Axon Framework doesn't necessarily mean an actual @EventHandler annotated function has been invoked.

    Hence, there is no guarantee on that matter and as such no Event Handler is given at this point.

    Additionally though, if the ErrorHandler is reached that typically means the transaction (the UnitOfWork) would be rolled back. Thus, for any event handler or event handling component, the operation would be adjusted.

    Hence, an exception entering the ErrorHandler essentially means all Event Handlers part of the Event Processor should be invoked again. As such knowing the exact instance wouldn't be an immediate necessity. Instead, you would require a process which [periodically / on invocation] checks the dead letter queue and invokes all handlers for the given Event Processor to process the given event again.

    That's my two cents to the scenario, hope this helps you out Juan.