简体   繁体   English

处理 controller 中的 CompletableFuture 异常

[英]Handling CompletableFuture exceptions in controller

I'm trying to get into CompletableFuture class for a project I'm running, and I got to some question here:我正在尝试为我正在运行的项目进入CompletableFuture class ,我在这里遇到了一些问题:

There is the following method: it tries to find a conversation by its ID or hash;有以下方法:通过ID或hash尝试查找会话; and, if not found, it throws an exception.如果没有找到,它会抛出异常。 So far, so good.到目前为止,一切都很好。

public ConversationOutput getConversationByIdOrHash(String conversationIdOrHash)
            throws ConversationNotFoundException {
        Conversation conversation = this.conversationRepository.getByIdOrHash(conversationIdOrHash);

        if (conversation == null) {
            throw new ConversationNotFoundException(conversationIdOrHash);
        }

        return this.modelMapper.map(conversation, ConversationOutput.class);
    }

Note that I am throwing ConversationNotFoundException from my method signature.请注意,我正在从我的方法签名中抛出ConversationNotFoundException My SpringBoot controller is reacting to this exception and it's all working fine since the beginning.我的 SpringBoot controller 正在对此异常做出反应,并且从一开始就一切正常。

What I'm trying to do is to make this to a CompletableFuture return and actually throwing an exception, something similar to:我想要做的是使它成为CompletableFuture返回并实际抛出异常,类似于:

    public CompletableFuture<ConversationOutput> getConversationByIdOrHashAsync(String conversationIdOrHash)
            throws ConversationNotFoundException {
        return CompletableFuture.supplyAsync(() -> this.getConversationByIdOrHash(conversationIdOrHash));
}

I've seen posts where people use exceptionally to handle exceptions, but what I really want to do is to throw it to my controller and let it handle it.我看过人们使用exceptionally处理异常的帖子,但我真正想做的是将它扔到我的 controller 并让它处理它。 Any suggestions of how can I make it?关于如何制作的任何建议?

Thank you all!谢谢你们!

The question is do you care about the result of CompletableFuture .问题是您是否关心CompletableFuture的结果。 CompletableFuture is like a special task and it is processed on other thread. CompletableFuture就像一个特殊的任务,它在其他线程上处理。 If you don't invoke .join() you won't receive the results of CompletableFuture .如果您不调用.join() ,您将不会收到CompletableFuture的结果。 This method also will propagate the exception if any occured.如果发生任何异常,此方法也将传播异常。 However it waits for CompletableFuture to finish and blocks the request.但是,它等待CompletableFuture完成并阻止请求。 However, there is no way to get exceptions from the inside of the CompletableFuture without waiting, you have to treat it like other task.但是,没有等待就无法从CompletableFuture内部获取异常,您必须像对待其他任务一样对待它。

You can pass the completed future in case of a success, and failed future along with your custom exception.如果成功,您可以传递已完成的未来,以及失败的未来以及您的自定义异常。

public CompletableFuture<ConversationOutput> getConversationByIdOrHashAsync(String conversationIdOrHash) {
        try {
            return CompletableFuture.completedFuture(this.getConversationByIdOrHash(conversationIdOrHash));
        } catch (ConversationNotFoundException e) {
            return CompletableFuture.failedFuture(e);
        }
    }

and then at your controller level you can handle the exception.然后在您的 controller 级别,您可以处理异常。

final CompletableFuture<ConversationOutput> future = getConversationByIdOrHashAsync("idOrHash");
        future.whenComplete((r, e) -> {
            if (e != null) {
                if (e instanceof ConversationNotFoundException) {
                    //handling
                }
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM