将隐式参数传递给Future.recover

时间:2016-05-06 02:32:34

标签: scala future implicit partialfunction

我想将隐式参数传递给我用recover我的Future s的部分函数。

def delete(id: Long) = ... { implicit something =>
  serviceLayer.doSomething(id).recover(errorHandler)
}

def errorHandler: PartialFunction[Throwable, Result] = {
    // I want to access the implicit parameter here
    case e@SomeException(message) => ... and here
    case _ => ... and here
}

1 个答案:

答案 0 :(得分:2)

然后,您errorHandler需要something作为implicit parameter收到:

def delete(id: Long) = ... { implicit something =>
  serviceLayer.doSomething(id).recover(errorHandler)
}

def errorHandler(implicit something: Something): PartialFunction[Throwable, Result] = {
    // Access something here
    case e@SomeException(message) => ... and here
    case _ => ... and here
}