Exception in command handler/event handler

I am using springboot application, I am sending command from my rest controller. I got an exception in command handler. If I am returning directly completable future i.e commandgateway.send, the exception is handling in exception handler of controller advice. But if I am not returning completable future, then the exception is not throwing to exception handler. How to handle this?

Working sample: return commandgateway.send(new UserCommand(id).thenApply(result-> new ResponseEntity<>(result,Httpstatus.nocontent);

Above returning completable future wrapping response entity.

Not working sample:
commandgateway.send(new UserCommand(id).thenAccept(result-> log.info(user created));
return ResponseEntity.nocontent.build();

Above returning directly responseentity

It’s a hunch based on your minimal sample, @Harshitha_Nalluri, but I think it has to do with the fact you’re not waiting on the completion of the CompletableFuture.

As you’re dealing with a Spring Boot application and using a REST controller, I assume the referred return is the return of a GET/POST/PUT/etc mapping.
If you return a CompletableFuture on any such methods, Spring will wait for the result for you.

Thus, it’s Spring’s logic/magic taking care of dealing with the result.

If you decide not to return the result from a CommandGateway#send on a REST controller endpoint method, it is your job to wait on the result.
Since if you don’t wait, the thread invoking the endpoint method will simply proceed, disregarding the task ran by the CompletableFuture.

Hence, you need to invoke CompletableFuture#get(), CompletableFuture#get(long, Timeout), or any other method that awaits the outcome of your CompletableFuture.
I hope that helps, @Harshitha_Nalluri!