Currently we use Axonhub/Axondb and need to deal with Exceptions from queryGateway and commandGateway.
The pattern we use is to defined custom exceptions which will be thrown by QueryHandlers or CommandHandlers,
resulting in CommandExecutionException or QueryExecutionException.
Typically, the ‘caller’ is a REST controller, which will handle the exception by a spring boot exception adviser.
Now, the original exception is lost, but the Exception message can be retrieved by unwrapping:
To deal with unwrapping and mapping to HTTP Status code, and also have a nicely formatted error message,
we made a utility, which can be used in a CompletableFuture.exceptionally(…) method. It can either respond directly or throw an
api exception which is handled by the Exception Adviser.
The cool thing about this is , that it doesn’t clutter the flow when chaining async calls.
Some exceptions are from the framework, i.e. Aggregate Seq. number violations or Aggregate not found exceptions, so those will
always need to be caught and mapped properly. (Like a 404 or something else… )
All things considers, this pattern works nicely for us, but we still doubt if this is the right pattern.
My question to the community is, how do people deal with this very typical issue when using Axon.
- Do not throw business exceptions, but put errors in Error objects returned by the CH or QH.
- Throw business exceptions and handle them as we do.
- Alternatives… ??
We would .be happy to share our utility, if anyone interested, please contact me privately).