Reputation: 255
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] = {
Upvotes: 0
Views: 55
Reputation: 255
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)
}
}
Upvotes: 0
Reputation: 9168
In your action function you can do:
serve(...).flatMap(serveSuccess => aCustomFutureRes).recoverWith { case error => aFutureResOnFailure }
Upvotes: 1