【问题标题】:io.reactivex.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Receivedio.reactivex.exceptions.CompositeException$CompositeExceptionCausalChain:CompositeException 的原因链按顺序收到
【发布时间】:2021-07-13 10:01:10
【问题描述】:

micronaut 中的自定义全局异常

public class GlobalException extends RuntimeException{
}

@Produces
@Singleton
@Requires(classes = {GlobalException.class, ExceptionHandler.class})
public class GlobalExceptionHandler implements ExceptionHandler<GlobalException, HttpResponse> {

    @Override
    public HttpResponse handle(HttpRequest request, GlobalException exception) {
        return HttpResponse.ok(0);
    }
}

从反应式 java 中抛出异常不工作,该异常必须被全局处理程序捕获。

Maybe<FindProductCommand> find(ProductSearchCriteriaCommand searchCriteria);

 public Maybe<FindProductCommand> get(ProductSearchCriteriaCommand searchCriteria) {
        return iProductManager.find(searchCriteria)
                .doOnError(throwable ->  { throw new GlobalException(); });
    }

CompositeException 通常在您的错误处理程序本身在处理流中抛出的错误时抛出异常时抛出,但我需要从错误处理程序中抛出异常

    io.reactivex.exceptions.CompositeException: 2 exceptions occurred. 
    at io.reactivex.internal.operators.maybe.MaybeOnErrorReturn$OnErrorReturnMaybeObserver.onError(MaybeOnErrorReturn.java:88)
    at io.micronaut.reactive.rxjava2.RxInstrumentedMaybeObserver.onError(RxInstrumentedMaybeObserver.java:58)
    at io.reactivex.internal.operators.flowable.FlowableElementAtMaybe$ElementAtSubscriber.onError(FlowableElementAtMaybe.java:94)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableOnErrorNext$OnErrorNextSubscriber.onError(FlowableOnErrorNext.java:90)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.subscriptions.EmptySubscription.error(EmptySubscription.java:55)
    at io.reactivex.internal.operators.flowable.FlowableError.subscribeActual(FlowableError.java:40)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:57)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.reactivex.internal.operators.flowable.FlowableOnErrorNext$OnErrorNextSubscriber.onError(FlowableOnErrorNext.java:115)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.maybe.MaybeToFlowable$MaybeToFlowableSubscriber.onError(MaybeToFlowable.java:75)
    at io.micronaut.reactive.rxjava2.RxInstrumentedMaybeObserver.onError(RxInstrumentedMaybeObserver.java:58)
    at io.reactivex.internal.operators.flowable.FlowableElementAtMaybe$ElementAtSubscriber.onError(FlowableElementAtMaybe.java:94)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.onError(FlowableSubscribeOn.java:102)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.checkTerminate(FlowableFlatMap.java:567)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.drainLoop(FlowableFlatMap.java:374)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.drain(FlowableFlatMap.java:366)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.onError(FlowableFlatMap.java:325)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableTimeoutTimed$TimeoutSubscriber.onTimeout(FlowableTimeoutTimed.java:139)
    at io.reactivex.internal.operators.flowable.FlowableTimeoutTimed$TimeoutTask.run(FlowableTimeoutTimed.java:170)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: io.reactivex.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
    at io.reactivex.exceptions.CompositeException.getCause(CompositeException.java:110)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:57)
    at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:119)
    at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:419)
    at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
    at ch.qos.logback.classic.Logger.error(Logger.java:538)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.logException(RoutingInBoundHandler.java:2162)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.writeDefaultErrorResponse(RoutingInBoundHandler.java:2139)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.exceptionCaughtInternal(RoutingInBoundHandler.java:426)
    at io.micronaut.http.server.netty.RoutingInBoundHandler.access$200(RoutingInBoundHandler.java:151)
    at io.micronaut.http.server.netty.RoutingInBoundHandler$5.doOnError(RoutingInBoundHandler.java:1408)
    at io.micronaut.core.async.subscriber.CompletionAwareSubscriber.onError(CompletionAwareSubscriber.java:63)
    at io.reactivex.internal.util.HalfSerializer.onError(HalfSerializer.java:70)
    at io.reactivex.internal.subscribers.StrictSubscriber.onError(StrictSubscriber.java:103)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty$SwitchIfEmptySubscriber.onError(FlowableSwitchIfEmpty.java:64)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.micronaut.http.server.netty.RoutingInBoundHandler$7.doOnError(RoutingInBoundHandler.java:1580)
    at io.micronaut.core.async.subscriber.CompletionAwareSubscriber.onError(CompletionAwareSubscriber.java:63)
    at io.micronaut.core.async.publisher.Publishers$2.doOnError(Publishers.java:270)
    at io.micronaut.core.async.subscriber.CompletionAwareSubscriber.onError(CompletionAwareSubscriber.java:63)
    at io.reactivex.internal.util.HalfSerializer.onError(HalfSerializer.java:70)
    at io.reactivex.internal.subscribers.StrictSubscriber.onError(StrictSubscriber.java:103)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.maybe.MaybeToFlowable$MaybeToFlowableSubscriber.onError(MaybeToFlowable.java:75)
    at io.micronaut.reactive.rxjava2.RxInstrumentedMaybeObserver.onError(RxInstrumentedMaybeObserver.java:58)
    ... 36 common frames omitted
Caused by: java.util.concurrent.TimeoutException: The source did not signal an event for 10000000000 nanoseconds and has been terminated.
    ... 10 common frames omitted
Caused by: fete.bird.common.extension.GlobalException: null
    at fete.bird.api.v1.controller.ProductController.lambda$get$0(ProductController.java:44)
    at io.reactivex.internal.operators.maybe.MaybeOnErrorReturn$OnErrorReturnMaybeObserver.onError(MaybeOnErrorReturn.java:85)
    at io.micronaut.reactive.rxjava2.RxInstrumentedMaybeObserver.onError(RxInstrumentedMaybeObserver.java:58)
    at io.reactivex.internal.operators.flowable.FlowableElementAtMaybe$ElementAtSubscriber.onError(FlowableElementAtMaybe.java:94)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableOnErrorNext$OnErrorNextSubscriber.onError(FlowableOnErrorNext.java:90)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.subscriptions.EmptySubscription.error(EmptySubscription.java:55)
    at io.reactivex.internal.operators.flowable.FlowableError.subscribeActual(FlowableError.java:40)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:57)
    at io.reactivex.Flowable.subscribe(Flowable.java:14918)
    at io.reactivex.Flowable.subscribe(Flowable.java:14865)
    at io.reactivex.internal.operators.flowable.FlowableOnErrorNext$OnErrorNextSubscriber.onError(FlowableOnErrorNext.java:115)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.maybe.MaybeToFlowable$MaybeToFlowableSubscriber.onError(MaybeToFlowable.java:75)
    at io.micronaut.reactive.rxjava2.RxInstrumentedMaybeObserver.onError(RxInstrumentedMaybeObserver.java:58)
    at io.reactivex.internal.operators.flowable.FlowableElementAtMaybe$ElementAtSubscriber.onError(FlowableElementAtMaybe.java:94)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.onError(FlowableSubscribeOn.java:102)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.checkTerminate(FlowableFlatMap.java:567)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.drainLoop(FlowableFlatMap.java:374)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.drain(FlowableFlatMap.java:366)
    at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.onError(FlowableFlatMap.java:325)
    at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onError(RxInstrumentedSubscriber.java:66)
    ... 10 common frames omitted

【问题讨论】:

标签: java rx-java rx-java2 micronaut reactivex


【解决方案1】:

实际抛出的异常是CompositeException,因此它与ExceptionHandler&lt;GlobalException, HttpResponse&gt; 不匹配。 Micronaut 没有对 CompositeException 进行特殊处理,即使有,我也不认为存在合理的默认值,因为存在多个异常但只应调用 1 个异常处理程序。

一般来说,你不应该这样做.doOnError(throwable -&gt; { throw new GlobalException(); });。相反,您可以使用 onErrorResumeNext 并返回一个可能会发出错误的新发布者。如果它发出错误,将收到错误而不是CompositeException

【讨论】:

  • 返回一个可能会发出错误的新发布者的示例将很有帮助
  • return iProductManager.find(searchCriteria) .onErrorResumeNext(throwable -> { return Maybe.error(new GlobalException(throwable));});为我工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-09
相关资源
最近更新 更多