簡體   English   中英

處理所有流的 onFailure

[英]Handle onFailure from all streams

在以下場景中,如果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!");
  });

問題是,如果錯誤發生在getSomething上,則不會調用onFailure並且我收到:

未處理的異步異常,發回 500

我試過使用onFailure().callonFailure().retry() ,甚至在開始時設置onFailure ,在中間,但似乎沒有任何效果。

如果我理解正確,getSomething,創建 stream。 但是在創建時 go 錯誤,例如 stream 永遠不會創建。

由於 stream 未創建,因此不會發生任何事情,因為 stream 不存在。 為了避免這種行為,我通常使用 voidItem 創建 stream 然后 map 以正確創建。 因此,創建中的錯誤將由 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 處理,而不是由創建代碼處理。

單的例子:

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

這樣,您應該能夠處理 stream 中的所有錯誤。 重要的是要了解,您需要創建 stream,否則您無法在其上運行 onFailure。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM