I have a long chain of completable futures in my project, with each step calling a backend API, which can give multiple error responses and one success response. Now, after parsing the response, I need to judge if it's an error, then I need to show to the user. I also need to know which stage in my chain, produced this error. My approach right now (shown below) is to throw a Runtime Exception whenever I encounter an error response, and then append exceptionally block to my chain. I feel that this is not the best way to do it, since a runtime exception doesn't fit in this scenario. It also makes my code ugly, since I have to do it whenever I process a response, leading to an extra exception check. Is there a better way to do it?

//some api call

Response response = request.send();

if(response.hasError()){ //this is what I am doing right now

logger.error("this is error response");

throw new ResponseErrorException("Error response received for request");



This is basically repeated for every step in the chain.

Summary: If I get a failure response in any of the steps in a CompletableFuture chain, what's a good way to propagate it to the user?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.