[英]How to handle unhandled exception throw in monix onErrorHandle
I am using monix tasks and i am trying to catch a Throwable and then convert to a custom error. 我正在使用monix任务,我正在尝试捕获Throwable然后转换为自定义错误。 I have removed/changed the code to be simple and relevant. 我删除/更改了代码以使其简单且相关。 This is the code (question follows after the code snippet): 这是代码(代码片段后面的问题):
import io.netty.handler.codec.http.HttpRequest
import monix.reactive.Observable
import io.netty.buffer.ByteBuf
import monix.eval.Task
import com.mypackage.Response
private[this] def handler(
request: HttpRequest,
body: Observable[ByteBuf]
): Task[Response] = {
val localPackage = for {
failfast <- Task.eval(1 / 0)
} yield failfast
// Failure case.
localPackage.onErrorRecoverWith {
case ex: ArithmeticException =>
print(s"LOG HERE^^^^^^^^^^^^^^^")
return Task.now(
Response(HttpResponseStatus.BAD_REQUEST,
None,
None)
)
}.runAsync
// Success case.
localPackage.map { x =>
x match {
case Right(cool) =>
Response(
HttpResponseStatus.OK,
None,
cool
)
case Left(doesntmatter) => ???
}
}
}
I am able to see the print statement but the expected Task.now(Response(...
is not being returned. Instead the method that calls the handler method is throwing an error. How do i make it return the Task[Response]
? 我能够看到print语句但是预期的Task.now(Response(...
没有被返回。相反,调用处理程序方法的方法抛出错误。如何让它返回Task[Response]
?
The success case works, the failure case does not. 成功案例有效,失败案例没有。
Edit #1 : Fix errors in scala code. 编辑#1:修复scala代码中的错误。
Edit #2 This is how I fixed it. 编辑#2这就是我修复它的方法。
// Success case.
localPackage.map { x =>
x match {
case Right(cool) =>
Response(
HttpResponseStatus.OK,
None,
cool
)
case Left(doesntmatter) => ???
}
}.onErrorRecoverWith {
case ex: ArithmeticException =>
print(s"LOG HERE^^^^^^^^^^^^^^^")
return Task.now(
Response(HttpResponseStatus.BAD_REQUEST,
None,
None)
)
}
I was thinking in terms of future and forgot the lazy eval
nature of task. 我在思考未来,忘记了任务的lazy eval
性。 Also I understood how the CancellableFuture
value was being discarded in the failure task. 我也理解了如何在失败任务中丢弃CancellableFuture
值。
Several problems with your sample. 你的样品有几个问题。
For one this code isn't valid Scala: 对于其中一个,此代码无效Scala:
val localPackage = for {
failfast <- 1 / 0
} yield failfast
I guess you meant Task.eval(1 / 0)
. 我猜你的意思是Task.eval(1 / 0)
。
Also onErrorHandle
does not have a Task
as a return type, you were probably thinking of onErrorHandleWith
. 另外onErrorHandle
没有Task
作为返回类型,你可能会想到onErrorHandleWith
。 And it's a pretty bad idea to give it a partial function (ie a function that can throw exceptions due to matching errors) — if you want to match on that error, then better alternatives are onErrorRecover
and onErrorRecoverWith
, which take partial functions as arguments. 给它一个部分函数(即一个可以因匹配错误而抛出异常的函数)是一个非常糟糕的主意 - 如果你想匹配那个错误,那么更好的选择是onErrorRecover
和onErrorRecoverWith
,它们将部分函数作为参数。
So here's a sample: 所以这是一个示例:
import monix.eval._
import monix.execution.Scheduler.Implicits.global
val task = Task.eval(1 / 0).onErrorRecoverWith {
case _: ArithmeticException => Task.now(Int.MinValue)
}
task.runAsync.foreach(println)
//=> -2147483648
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.