0

MongoController provides serve function to serve the result of a query (as a Cursor). I just want to do something different than letting serve return NotFound, like sending some other default file. I'm wondering if it is possible to use pattern matching to check the result. The signature is this:

/** Returns a future Result that serves the first matched file, or NotFound. */
  def serve[T <: ReadFile[_ <: BSONValue], Structure, Reader[_], Writer[_]](gfs: GridFS[Structure, Reader, Writer], foundFile: Cursor[T], dispositionMode: String = CONTENT_DISPOSITION_ATTACHMENT)(implicit ec: ExecutionContext): Future[SimpleResult] = {
Dario
  • 255
  • 1
  • 10

2 Answers2

1

In your action function you can do:

serve(...).flatMap(serveSuccess => aCustomFutureRes).recoverWith { case error => aFutureResOnFailure }
cchantep
  • 9,118
  • 3
  • 30
  • 41
  • That actually enters every time to flatMap body, never to recover (even when the file is not found – Dario Jun 17 '15 at 22:19
  • Ok, I think I've found something very close to a solution for my original question: `serve( gridFS, gridFS.find(query), CONTENT_DISPOSITION_INLINE) .flatMap{ result => result match { case Ok => Future.successful(result) case NotFound => Assets.at("/public","/images/not-found.gif").apply(request) } }` – Dario Jun 17 '15 at 22:42
0

Finally, found this solution:

serve(
  gridFS, 
  gridFS.find(query),
  CONTENT_DISPOSITION_INLINE)
.flatMap{ result => 
  result match {
    case NotFound => // Handle file not found
    case _ => Future.successful(result)
  }
}
Dario
  • 255
  • 1
  • 10
  • Even more: it can be used flatMap result to match its header.status with any Result.{anyResultTpe}.status, like `result.header.status match { case Ok.header.status => // handle ok` – Dario Jun 18 '15 at 12:49