How can I use @ExceptionHandler annotation in saga

I tried use it in saga, but it didn’t trigger the ExceptionHandler even though the exception and stack trace was shown on the log, I wonder why. Here’s how the code looks like.

class TestSaga {

  @StartSaga
  @SagaEventHandler(associationProperty = "test")
  fun on(event: EventA) {
    Mono.error(IllegalArgumentException("Test handling from A"))
      .block()!!
  }
  
  @SagaEventHandler(associationProperty = "test")
  fun on(event: EventB) {
    Mono.error(IllegalArgumentException("Test handling from B"))
      .block()!!
  }

  @SagaEventHandler(associationProperty = "test")
  fun on(event: EventC) {
    Mono.error(IllegalArgumentException("Test handling from C"))
      .block()!!
  }
  

  @ExceptionHandler
  fun on(error: IllegalArgumentException) {
    // doesn't trigger this handler
    println("Exception handler tested")
  }
}

I use axonframework version 4.4.8 and also use TrackingEventProcessor

That’s curious what you’re experiencing, @xXExpertAxonUserXx!
I’ve got a couple of things you can try out to see where the predicament lies:

  • Have you tried just throwing the exception directly from the @SageEventHandler block instead of wrapping it in a Mono?
  • Have you tried an @ExceptionHandler annotated method that doesn’t take any parameter?
  • Have you tried an @ExceptionHandler(resultType = IllegalArgumentException.class) annotated method?
  • Have you tried using the latest version of Axon Framework? It’s currently on version 4.5.10.

Any news on this ?

I have an exceptionhandler in my saga that looks like this:

@ExceptionHandler
public void error(Exception exception) {
    eventGateway.publish(ExceptionEvent.create(exception));
}

With a @SagaEventHandler in which a NullPointerException is thrown.

Just switched to axon version 4.7.3 to be on the latest version.

Is there something else I need to configure to get saga exception handling to work ?

I actually picked this up recently. So currently it does not work. And it will be going to work in 4.8. We might consider it a bug and also have the required changes in 4.7, not sure on that one through.

Thanks for letting this know ! That’s already better than trying to figure out what I might have wrongly configured.

In an attempt to get a better understanding after reading the axon documentation, today I listened to the following interesting podcast: Podcast Exploring Axon: Season 2 Episode 7 - Exception Handling in Axon Framework - YouTube Which also states that it should work for all kind of message handlers. So it could be considered a bug I guess.

I’d appreciate it if this could be taken up as a bug. But I don’t know when the 4.8 release is planned? It would be great to have it before our release in May. Otherwise I have to figure out something else.

Also, would the exception handler annotation be testable using the axon test fixtures ? Cause that didn’t seem to be supported in the past: Testing a @ExceptionHandler with a Saga - #2 by vab2048

1 Like

Was there an Axon version where this feature did work? Maybe I can just switch back to that version.

Not AFAIK, the annotation is relatively new, and the code for using it in Saga’s was just kind of missing. We have an internal meeting next Thursday. Once it’s added to the 4.7 branch we could have a release pretty soon after. We kind of plan to have 3 minor releases each year, which means the 4.8 should be around May.

Ok, thanks a lot for your fast feedback. Really appreciate it !

Seems like this can be implemented with the ListenerInvocationErrorHandler. So I got it somewhat working. Although it would still be nice to have it work based on the annotation.

We discussed this today, and based on the changes needed, and because it’s not a bug in the traditional sense, we decided to move it to 4.8.0. That will be out hopefully in May or June, with, among other things, some improvements to monitoring and dead letter queue. This is the related PR if you are interested.