This Play for Scala method (that should return Future[play.api.mvc.Result]
) invokes another method run
that is a future that may throw an exception. If run
doesn't throw an exception, then the method should return a Json with ret
equals 0
. If run
does throw an exception, then the method should retun a Json with ret
equals 1
.
Problem is that the OnComplete
is throwing a compilation error:
type mismatch; found : Unit required: scala.concurrent.Future[play.api.mvc.Result]
how to fix this?
def test (req:Int) : Future[play.api.mvc.Result] = {
val future = run(req)
future.map {
result =>
val json = JsObject(Seq(
"rs" -> Json.toJson(result),
"ret" -> JsString("0")
))
Ok(json)
}
future.onComplete { // <-- compilation error
case Success(_) => // do nothing
case Failure(ex) => {
val json = JsObject(Seq(
"msg" -> JsString(ex.getMessage),
"ret" -> JsString("1")
))
Ok(json)
}
}
}