如何在monix onErrorHandle中处理未处理的异常抛出

时间:2017-11-19 23:06:38

标签: scala reactive-programming monix

我正在使用monix任务,我正在尝试捕获Throwable然后转换为自定义错误。我删除/更改了代码以使其简单且相关。这是代码(问题在代码片段之后):

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) => ???
      }
  }
}

我能够看到print语句但是没有返回预期的Task.now(Response(...。相反,调用处理程序方法的方法是抛出错误。如何让它返回Task[Response]

成功案例有效,失败案例没有。

编辑#1:修复scala代码中的错误。

编辑#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)
          )
    }

我在思考未来并忘记了任务的lazy eval性质。我也理解了在失败任务中如何丢弃CancellableFuture值。

1 个答案:

答案 0 :(得分:3)

Several problems with your sample.

For one this code isn't valid Scala:

val localPackage = for {
  failfast <- 1 / 0
} yield failfast

I guess you meant Task.eval(1 / 0).

Also onErrorHandle does not have a Task as a return type, you were probably thinking of onErrorHandleWith. And it's a pretty bad idea to give it a partial function (i.e. 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.

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.