为什么Mono.doOnSuccess()会抑制错误?

问题描述 投票:0回答:1

在下面的代码片段中,抛出异常,如预期的那样:

@Test
public void testError()
{
    Mono.error(new NullPointerException())
        .subscribe();
}

但是,在以下代码段中不会抛出任何异常:

@Test
public void testErrorWithDoOnSuccess()
{
    Mono.error(new NullPointerException())
        .doOnSuccess(aValue -> {})
        .subscribe();
}

当我使用块运算符而不是subscribe运算符时,异常再次起作用:

@Test
public void testErrorWithDoOnSuccessAndBlock()
{
    Mono.error(new NullPointerException())
        .doOnSuccess(aBoolean -> {})
        .block();
}

Mono.doOnSuccess运算符的文档没有具体说明它的行为w.r.t.错误,但我不希望这个行为给定运营商的名称。为什么它会抑制异常(只有在不使用块运算符时)?这是预期的行为,还是这个错误?

使用的反应堆版本是撰写本文时最新的版本,3.1.8.RELEASE

project-reactor
1个回答
0
投票

这是一个错误,只有在你使用doOnTerminate / doAfterTerminate,而不是doOnSuccess的情况下,运算符应该禁止“无错误回调实现”异常...

也就是说,运算符仍然传播NPE,并且通过实现最佳实践可以轻松解决此错误:始终至少在subscribe(...)中定义值处理程序和错误处理程序。

© www.soinside.com 2019 - 2024. All rights reserved.