0

Starting with the example from this post, I'm wondering how to deal with recover:

def HasToken(action: String => EssentialAction): EssentialAction = EssentialAction { requestHeader =>
  val maybeToken = requestHeader.headers.get("session")

  val futureIteratee: Future[Iteratee[Array[Byte], SimpleResult]] = maybeToken map { token =>
    //This returns a future...
    Session.find(token).map {
      case Some(session) => action(session)(requestHeader)
      case None => Done[Array[Byte], Result](Unauthorized("Invalid token"))
    }.recover {
      Done[Array[Byte], Result](Unauthorized("400 Error finding Security Token\n"))
    }
  } getOrElse {
    Future.successful(Done[Array[Byte], Result](Unauthorized("401 No Security Token\n")))
  }

  Iteratee.flatten(futureIteratee)
}

The code above doesn't compile and I always get the following error message:

[error] /home/j3d/projects/test/app/Security.scala:80: type mismatch;
[error]  found   : scala.concurrent.Future[play.api.libs.iteratee.Iteratee[_4,play.api.mvc.Result] forSome { type _4 >: _3 <: Array[Byte]; type _3 <: Array[Byte] }]
[error]  required: scala.concurrent.Future[play.api.libs.iteratee.Iteratee[Array[Byte],play.api.mvc.Result]]
[error]         } recover {
[error]           ^
[error] one error found
[error] (compile:compile) Compilation failed

Am I missing something?

Community
  • 1
  • 1
j3d
  • 9,492
  • 22
  • 88
  • 172

1 Answers1

1

Oddly enough, I thought that the problem might've been that the function wasn't inferring the proper type you needed. This has happened to me in the past where the result type depended on an existing generic type, e.g. U >: T.

After trying your code snippet though, I noticed that you didn't use a partial function for the recover call, and after I fixed that it compiled without any problems.

Session.find(token).map {
    case Some(session) => action(session)(requestHeader)
    case None => Done[Array[Byte], Result](Unauthorized("Invalid token"))
}.recover {
    case _ =>
        Done[Array[Byte], Result](Unauthorized("400 Error finding Security Token\n"))
}