Mono.subscribe()向客户端抛出异常



我一直试图找出运行时异常没有传播回客户端的原因。我有下一段代码,所以当我返回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$0(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.

事先非常感谢。顺致敬意,

我一直试图找出运行时异常没有传播回客户端的原因。

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

如果删除该链上的subscribe()调用,将方法更改为返回Mono<Boolean>,然后返回该方法中的整个链,则该方法应按预期工作。

最新更新