提问者:小点点

如何处理CompletableFuture.supplyAsync抛出的异常


我有下面的代码块。我得到的是org.springframework.web.context.request.async.AsyncRequestTimeoutException的catch块没有处理它。有人能告诉我如何处理下面的Supply yAsync块抛出的异常吗?

 @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);
        });

}

共1个答案

匿名用户

也许是这样的(你必须调整类型):

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