防止RxJava将自定义异常包装为复合异常

防止RxJava将自定义异常包装为复合异常,java,exception,rx-java,Java,Exception,Rx Java,我的任务是使用RxJava进行实现,并以这种方式进行保护,如果发生任何错误,它将被包装到自定义异常中 fun testSummarizationException() { try { summarizeConfirmedTransactions() } catch (ex: Exception) { val customException = (ex as? CompositeException)?.exceptions?.get(0)

我的任务是使用RxJava进行实现,并以这种方式进行保护,如果发生任何错误,它将被包装到自定义异常中

fun testSummarizationException() {
    try {
        summarizeConfirmedTransactions()
    } catch (ex: Exception) {
        val customException = (ex as? CompositeException)?.exceptions?.get(0)
        //  assert to make sure thrown exception is of custom type
        assert(customException is SummarizationException)
    }
}
问题是,不管我做什么,RxJavaPlugin决定将我的异常包装成CompositeException,即使只有一个异常。它无法通过测试

我已经尝试了所有我能在google上找到的东西,一直到覆盖RxJavaPlugin的全局错误处理程序,但它忽略了尝试的更改

在写这篇文章的时候应该抛出它的函数的实现

Single SummaryConfirmedTransactions()引发SummaryException异常{
试一试{
Observable observableTransactions=transactions.get()
.doon错误(可丢弃->{
例外。传播(可丢弃);
});
可观测的可观测确认=确认。获取()
.doon错误(可丢弃->{
例外。传播(可丢弃);
});
return Observable.zip(observableTransactions,observableConfirmations,(t,c)->新的confirformableTransaction(t.transactionId,c.isconfirfied,t.value))
.filter(confirmableTransaction->confirmableTransaction.isconfirm)
.map(t->t.value)
.reduce(BigDecimal::add)
.toSingle()
.doon错误(可丢弃->{
例外。传播(可丢弃);
});
}捕获(例外e)
{
抛出新的SummariationException(例如getMessage());
}
测试中的断言失败,因为最终抛出的异常是CompositeException,其中只有一个异常。我需要它是SummationException.class

非常感谢

编辑: 根据要求,以下是用于测试解决方案的代码。我不允许修改此代码

    @Test
    public void shouldWrapErrorIntoException() {
        final ConfirmedTransactionSummarizer summarizer =
                new ConfirmedTransactionSummarizer(ALL_CONFIRMED::transactions, () -> Observable.error(new RuntimeException("Booom")));


        summarizer
                .summarizeConfirmedTransactions()
                .subscribe(testObserver);

        testObserver.assertError(SummarizationException.class);
        testObserver.assertErrorMessage("Booom");
    }

另外,我已经问过任务的执行者,他说“我是唯一有这个问题的人”我不应该使事情过于复杂,而应该寻求最简单的解决方案……这会导致3个异常的复合异常——其中一个是我的异常包装,另外两个是测试代码插入的RuntimeException实例。

处理这一问题的一种方法是在测试中使用try-catch块,取消复合异常,然后使用ass埃尔廷发现了一个例外

fun testSummarizationException() {
    try {
        summarizeConfirmedTransactions()
    } catch (ex: Exception) {
        val customException = (ex as? CompositeException)?.exceptions?.get(0)
        //  assert to make sure thrown exception is of custom type
        assert(customException is SummarizationException)
    }
}
这是打开CompositeException以获取自定义异常的地方

fun testSummarizationException() {
    try {
        summarizeConfirmedTransactions()
    } catch (ex: Exception) {
        val customException = (ex as? CompositeException)?.exceptions?.get(0)
        //  assert to make sure thrown exception is of custom type
        assert(customException is SummarizationException)
    }
}
val customException=(例如作为?CompositeeException)?.Exception?获取(0)


如果允许,则将异常类型转换为CompositeException。如果不允许对该类型进行转换,则将返回null并失败测试。

好的,因此在进一步挖掘之后,并根据朋友提供的有用提示,我已成功确定了任务的意图:

我应该在那里做的是:

Single SummaryConfirmedTransactions()引发SummaryException异常{
Observable observableTransactions=transactions.get();
可观测的可观测确认=确认。get();
返回Observable.zip(observableTransactions、observableConfirmations、,
(t,c)->新的可确认交易(c.isconfirfied,t.value))
.filter(confirmableTransaction->confirmableTransaction.isconfirm)
.map(t->t.value)
.reduce(BigDecimal::add)
.toSingle()
.onErrorResumeNext(th->Single.error(新摘要异常(th.getMessage())));
}
感谢大家的见解,案例结束:)


tl:医生,我不应该“裹着”错误转换为抛出的异常,但将它们包装为包含异常的错误响应。

为什么您不能在我的代码中打开
CompositeeException
?永远无法在我的代码中看到它-错误处理被Rx stuff抢走,在我的任何处理都可以得到一个词之前…不幸的是,我不允许在这个任务中修改测试。只是有趣的动作测试是测试。你能在这里发布测试代码吗?这将有助于理解你的问题。
fun testSummarizationException() {
    try {
        summarizeConfirmedTransactions()
    } catch (ex: Exception) {
        val customException = (ex as? CompositeException)?.exceptions?.get(0)
        //  assert to make sure thrown exception is of custom type
        assert(customException is SummarizationException)
    }
}