scala - How to recover an error from a Future in an EssentialAction -
starting example this post, i'm wondering how deal 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 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 , 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] 1 error found [error] (compile:compile) compilation failed am missing something?
oddly enough, thought problem might've been function wasn't inferring proper type needed. has happened me in past result type depended on existing generic type, e.g. u >: t.
after trying code snippet though, noticed didn't use partial function recover call, , after fixed compiled without 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")) }
Comments
Post a Comment