【问题标题】:Is there a way to merge multiple mono error signals?有没有办法合并多个单声道错误信号?
【发布时间】:2020-09-15 17:15:09
【问题描述】:

有没有办法合并多个错误信号?例如:

    return Mono.zipDelayError(
        monoOne(), //throws ValidationException with list of validation details 1
        monoTwo(),
        monoThree() //throws ValidationException with list of validation details 2
    )
    .then();
}

因此,我想返回 ValidationException 和合并的验证详细信息列表

【问题讨论】:

    标签: java mono spring-webflux project-reactor flux


    【解决方案1】:

    您可以使用Exceptions.unwrapMultiple() 实用程序方法获取List<Throwable>,然后将该列表缩减为单个ValidationException(或执行您喜欢的任何其他检查/处理。)

    那么就只是将上面的内容包裹在onErrorMap()中:

    Mono.zipDelayError(
            Mono.error(new ValidationException("Reason 1")),
            Mono.just("ok"),
            Mono.error(new ValidationException("Reason 2"))
    )
    .onErrorMap(e ->
            Exceptions.unwrapMultiple(e).stream()
                    .reduce((e1, e2) -> new ValidationException(String.join(", ", e1.getMessage(), e2.getMessage()))).get()
    );
    

    ...给出:

    Exception in thread "main" reactor.core.Exceptions$ReactiveException: ValidationException: Reason 1, Reason 2
    

    请注意,Exceptions.unwrapMultiple() 仍然适用于异常不是倍数的情况 - 在这种情况下,您只会得到一个单例列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-08
      • 2018-02-02
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 2020-03-05
      • 2017-12-11
      • 2013-12-14
      相关资源
      最近更新 更多