Returning command errors over a rest API

I am using Spring Boot to create a restful API that takes commands in. How do I return any validation errors that happen on the aggregate? For example, if I have a property on the aggregate called inactive and I can only call deactivate command if the aggregate is currently active and if its not, I want to throw AggregateAlreadyDeactivatedException but return it as error in spring boot restful service…how can I possibly do this.



one way is to use a callback to listen for the result of command handling. Either the onSuccess or the onFailure method is called, depending on what happened during processing. You can use a FutureCallback to be able to wait for the result (regardless of the CommandBus being used is sync or async).
Alternatively, you can use the command gateway to send and wait in a single method call. You can also define custom command gateway interfaces. The documentation contains details on how this works.




I find it interesting, so i’m going to jump in,

@Allard i would find it also interesting to use a command interceptor for this particular ‘validation’, please correct me if necessary as ‘i are still n00b’.

@Chirdeep to honor REST principles, i wouldn’t ‘design’ anything that ‘waits’ for something. ‘Accept’ the request (e.g. 202) via your REST resource URI (e.g. the POST or PUT) and poll later for status.

I’m still learning a lot in Axon, but i am working in a quite complex implementation using it and never saw this ‘pattern’ being implemented by already experienced devs. What i’m saying is, commands are dispatched and the event handler will back-off if errors (in spring - that’s JPA Repository in my case)

Also embracing CQRS principles the aggregate shouldn’t inspect other ‘systems’ in order to create for itself a ‘big-picture’. Also, correct me if necessary.


Discusions can only get interesting if people jump in…

This type of validation cannot be done in an interceptor. The question was about using aggregate state to validate a command, which is only possible in the aggregate itself. Structural validation, on the other hand, is best done in an interceptor. This type of validation looks at the type of information present in the command, blocking it if is doesn’t conform to a basic set of rules.

Waiting is evil. Personally, I prefer to use the Servlet 3 Async mechanism to send a result asynchronousy when available. I am not a fan of polling for results. The overhead of polling is large, both in server processing, as in development time. By simply blocking the HTTP result until the command has been processed, you don’t have this overhead. Just to make sure: I am talking about command processing. The handling of events in projections is not included in this process.



Thanks a lot Allard, I will look into callback mechanism, I don’t like the idea of polling either.