简体   繁体   English

处理所有流的 onFailure

[英]Handle onFailure from all streams

In the following scenario, if an error happens on doSomething the onFailure works correctly:在以下场景中,如果doSomething发生错误,则onFailure会正常工作:

this.restClient.getSomething()
  .flatMap((something) -> {
    return this.restClient.doSomething();
  })
  .flatMap((res) -> {
    return Uni.createFrom().item(Response.ok().build());
  }).onFailure().transform((e) -> {
    LOG.error("Failed with", e);
    throw new InternalServerErrorException("Failed!");
  });

Problem is, if the error happens on getSomething the onFailure isn't called and I receive:问题是,如果错误发生在getSomething上,则不会调用onFailure并且我收到:

Unhandled asynchronous exception, sending back 500未处理的异步异常,发回 500

I've tried using onFailure().call , onFailure().retry() and even set the onFailure at the start, in the middle, but nothing seems to work out.我试过使用onFailure().callonFailure().retry() ,甚至在开始时设置onFailure ,在中间,但似乎没有任何效果。

If I understand it correctly, getSomething, create the stream.如果我理解正确,getSomething,创建 stream。 But at creation something go wrong, eg the stream is never created.但是在创建时 go 错误,例如 stream 永远不会创建。

As the stream is not create, onAnything can not happen, as the stream is not existing.由于 stream 未创建,因此不会发生任何事情,因为 stream 不存在。 To avoid this behavior, I usually create the stream with voidItem and then map it to the correct creation.为了避免这种行为,我通常使用 voidItem 创建 stream 然后 map 以正确创建。 So that error in creation will be handled from the stream, and not from creation code.因此,创建中的错误将由 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 处理,而不是由创建代码处理。

Example for uni:单的例子:

Uni.createFrom().voidItem()
   .map( voidItem -> functionForRealValue)
   .onFailure(handleFailure)

This way, you should be able to handle all errors in the stream.这样,您应该能够处理 stream 中的所有错误。 It's important to understand, that you need to create the stream, otherwise you can not run onFailure on it.重要的是要了解,您需要创建 stream,否则您无法在其上运行 onFailure。

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

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