Matching result of MongoController.serve

66 views Asked by At

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] = {
2

There are 2 answers

2
cchantep On

In your action function you can do:

serve(...).flatMap(serveSuccess => aCustomFutureRes).recoverWith { case error => aFutureResOnFailure }
1
Dario On

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)
  }
}