scalafutureimplicitpartialfunction

Passing an implicit parameter to Future.recover


I want to pass an implicit parameter to a partial function I use to recover my Futures.

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
}

Solution

  • Then your errorHandler needs to receive something as an 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
    }