Proper handling of "aggregate was not found" with axon server

Hello,
I’m trying to set up an app with axon server as an event store. I have an aggregate that is accessible via Spring MVC Controller with REST endpoints. I have a problem with handling a situation when an ID provided in my REST resource endpoint does not exists as an aggregate.
I would like to return 404 code from a spring in @ExceptionHandler but it looks like that I’m only able to catch CommandExecutionException that can correspond to any axon related issues.

What I see is that I receive a proper message from axon however its a response from axon server wrapped by some general Exception. Is there a clean way how i can determine that the cause of axon failure was f.g. AggregateNotFoundException?

Hey Jakub,

When in a distributed scenario, there’s typically no guarantee that the dispatching instance has the returned exception type on it’s classpath.
Hence Axon will map the thrown exception to a “HandlerExecutionException” instance, which can be either the CommandExecutionException or the QueryExecutionException.
These HandlerExecutionException allow the addition of a “details Object”, so that the end user can specify a form of exception handling contract within their own application.

For example by defining an enumeration containing your application’s error codes.

Best way to achieve this would be to introduce a dedicated MessageHandlerInterceptor which catches the exception and maps it to a CommandExecutionException with the desired details attached to it.

Granted, the AggregateNotFoundException is an Axon exception; might be something to be done here to automatically fill in these details…
FYI, the Reference Guide currently states the following about this concept.

Hope this helps you out Jakub!

Cheers,
Steven