Mono.subscribe() 将异常抛回给客户端

Mono.subscribe() to throw an exception back to the client

我试图找出运行时异常未传播回客户端的原因。我有下一段代码,所以当我 return a Mono.error 这应该在订阅错误部分处理,向客户端抛出异常,但这并没有发生。知道我做错了吗?

    public void onmethod(EventDetails eventDetails, String eventType) {
        messageConverter.convertAndSendMessage(eventType, eventDetails)
                .flatMap(aBoolean -> {
                    if (aBoolean)
                        log.debug("Event published");
                    else {
                        log.debug("Problem publishing event.");
                        return Mono.error(new RuntimeException("Problem publishing event."));
                    }
                    return Mono.just(true);
                })
                .doOnError(throwable -> log.error("Failed to consume message", throwable))
                .subscribe(
                        next -> { } ,
                        error -> {
                            throw Exceptions.propagate(error);
                        }
                );
}

这是我必须验证方法行为的测试。由于抛出任何异常,此测试失败。但是,我可以在日志中看到异常发生。

        Assertions.assertThrows(RuntimeException.class, () ->
                consentsListener.onmethod(
                        eventDetails, "eventType")
        );
19:44:32.463 [main] ERROR events.auth.ConsentsListenerImpl - Failed to consume message
java.lang.RuntimeException: Problem publishing event. 
    at events.auth.ConsentsListenerImpl.lambda$publishMessage[=13=](ConsentsListenerImpl.java:121)
    at reactor.core.publisher.FluxFlatMap.trySubscribeScalarMap(FluxFlatMap.java:152)
    at reactor.core.publisher.MonoFlatMap.subscribeOrReturn(MonoFlatMap.java:53)
    at reactor.core.publisher.Mono.subscribe(Mono.java:4084)
    at reactor.core.publisher.Mono.subscribeWith(Mono.java:4214)
    at reactor.core.publisher.Mono.subscribe(Mono.java:4070)
    at reactor.core.publisher.Mono.subscribe(Mono.java:4006)
    at reactor.core.publisher.Mono.subscribe(Mono.java:3978)
    at ...

org.opentest4j.AssertionFailedError: Expected java.lang.RuntimeException to be thrown, but nothing was thrown.

非常感谢您。 最好的问候。

I have been tried to find out why the Runtime exception is not propagated back to the client.

因为您订阅了它,这几乎肯定是错误的做法。框架(在本例中为 Webflux)应该控制对发布者的订阅。

如果您删除对该链的 subscribe() 调用,请将您的方法更改为 return Mono<Boolean> 然后 return该方法中的整个链,它应该按预期工作。