如何获取导致 Akka Streams 失败的对象? [英] How to get object that caused failure in Akka Streams?
问题描述
根据 akka 流文档 可以通过定义将 Throwable
映射到 Strategy
的决策器来处理流失败:
According to akka streams docs one can handle stream failure by defining a decider that maps a Throwable
to a Strategy
:
val decider: Supervision.Decider = {
case _: ArithmeticException => Supervision.Resume
case _ => Supervision.Stop
}
我想知道是否有办法访问导致错误的元素.当然,这个元素的类型是未知的,但是有没有办法把它作为Object
的实例来获取?
I wonder if there is a way to also get access to the element that caused the error. Of course, the type of this element is unknown, but is there a way to get it even as an instance of Object
?
推荐答案
最简单的方法是在抛出位置附近捕获 ArithmeticException
并抛出您的自定义异常 MyDetailedArithmeticException(culprit:Int)
将打印导致消息中出现问题的 int
.
The trivial way would be to catch the ArithmeticException
near where it is thrown and throw your custom exception MyDetailedArithmeticException(culprit: Int)
that would print the int
that caused the problem in the message.
这篇关于如何获取导致 Akka Streams 失败的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!