Checked exceptions on a custom Command Gateway

Hi,

I’m using a custom gateway with a checked exception, but I’m receiving a CommandExecutionException.

The documentation states the following:
https://docs.axoniq.io/reference-guide/configuring-infrastructure-components/command-processing/command-dispatching#creating-a-custom-command-gateway
"Any declared checked exception will be thrown if the command handler (or an interceptor) threw an exception of that type. "

I have the following custom gateway interface

public interface TraderCommandGateway {
void send(WithdrawMoneyCommand command) throws InsufficientFunds;
}

This is my command handler

@CommandHandler
public void handle(WithdrawMoneyCommand command) throws InsufficientFunds {
if (balance.compareTo(command.getAmount()) < 0 ) {
throw new InsufficientFunds(String.format(“Balance of %s is insufficient to withdraw %s”, balance, command.getAmount()));
}
apply(new MoneyWithdrawnEvent(command.getPortfolioId(), command.getAmount()));
}

And this is the exception class

public class InsufficientFunds extends Exception {

public InsufficientFunds(String message) {
super(message);
}
}

I’m using Axon 4.3.3. Is this a bug?

Same issue here, almost same setup in other domain, using Axon 4.3.5

Hi Robert and Martijn,

The reason you are receiving a CommandExecutionException is because you are in a distributed environment.
We have taken the stance with the framework that if the exception needs to go back over the wire to the original dispatcher, that we cannot assume that client has that exception on its classpath.
As such, it’s adjusted to a CommandExecutionException, of which we are certain can be handled.

If you want to share exception details or information, you will have to catch this exception and throw the CommandExecutionException yourself, adding a details object to the constructor.
These details could be a simple message, or maybe a shared status code solution within your domain (not too different from HTTP status codes for example).

Cleanest way to achieve this would be to introduce a MessageHandlerInterceptor which catches the exception thrown by a command handler.
This interceptor can interpret the exception into the details object given to the CommandExecutionException you would then throw.

Hope this helps you further Robert and Martijn!

Cheers,
Steven

PS. It is important to note that this mailing list will be discontinued as specified in this thread.
The thread also specifies where to look further for help when it comes to Axon as soon as this mailing list is closed.