How to handle exceptions and translate them to HTTP status codes?

Hi Alex,

we have a quite similar setup and what we usually do in the REST controller is using the org.axonframework.commandhandling.gateway.CommandGateway#sendAndWait() variant of the command gateway to send a command and synchronousy wait for the command to complete or throw an exception. We have a central class containing spring web exception handlers for all known exceptions to expect (see Web on Servlet Stack) that transforms an exception into a ResponseEntity with the appropriate status code. Since we’re using Axon Server to distribute command handling we implemented a MessageHandlerInterceptor that allows forwarding exception details from remote command handler to the controller (also see this thread: Get the entire stack trace fpr remote command handlers)

If you would use the org.axonframework.commandhandling.gateway.CommandGateway#send() variant of the command gateway to send the commands in your controller, you would need to block until the completeable future completes to get the exceptional result as described by @allardbz or e.g. return a HTTP 202 accepted response status code to indicate only that the command is submitted (allowing the outcome to be queried with a separate request on the query side).

1 Like