J-J
J-J

Reputation: 5871

How to handle the exceptions thrown by CompletableFuture.supplyAsync

I have the following block of code. I am getting org.springframework.web.context.request.async.AsyncRequestTimeoutException the catch block is not handling that. Can anyone tell me how to handle the exception thrown by the below supplyAsync block?

 @org.springframework.scheduling.annotation.Async
    public CompletableFuture<ResponseEntity<?>> getTeam(String teamCode) {
        CompletableFuture.supplyAsync(() -> {
            CricketTeam team;
            try {
                team = service.getTeamInfo(teamCode);
            } catch (Exception ex) {
                Error error = new Error(500, ex.getMessage());
                return new ResponseEntity<>(error, HttpStatus.OK);
            }
            return new ResponseEntity<>(team, HttpStatus.OK);
        });

}

Upvotes: 1

Views: 2847

Answers (1)

Evil Toad
Evil Toad

Reputation: 3242

Maybe something like this (you'll have to adjust the types):

    CompletableFuture.supplyAsync(() -> possiblyFailingMethod())
            .exceptionally((e) ->
                    {
                        log.error("Something went wrong", e);
                        return "KO";
            });

Upvotes: 5

Related Questions