0

Here below is my code to find a document by ObjectID:

def find(selector: JsValue, projection: Option[JsValue], sort: Option[JsValue],
  page: Int, perPage: Int): Future[Seq[JsValue]] = {

  var query = collection.genericQueryBuilder.query(selector).options(
    QueryOpts(skipN = page * perPage)
  )

  projection.map(value => query = query.projection(value))
  sort.map(value => query = query.sort(value.as[JsObject]))

  // this is the line where the call crashes
  query.cursor[JsValue].collect[Vector](perPage).transform(
    success => success,
    failure => failure match {
      case e: LastError => DaoException(e.message, Some(DATABASE_ERROR))
    }
  )
}

Now let's suppose we invoke this method with an invalid ObjectID:

// ObjectId 53125e9c2004006d04b605abK is invalid (ends with a K)
find(Json.obj("_id" -> Json.obj("$oid" -> "53125e9c2004006d04b605abK")), None, None, 0, 25)

The call above causes the following exception when executing query.cursor[JsValue].collect[Vector](perPage) in the find method:

Caused by: java.util.NoSuchElementException: JsError.get
        at play.api.libs.json.JsError.get(JsResult.scala:11) ~[play-json_2.10.jar:2.2.1]
        at play.api.libs.json.JsError.get(JsResult.scala:10) ~[play-json_2.10.jar:2.2.1]
        at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:44) ~[play2-reactivemongo_2.10-0.10.2.jar:0.10.2]
        at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:42) ~[play2-reactivemongo_2.10-0.10.2.jar:0.10.2]
        at reactivemongo.api.collections.GenericQueryBuilder$class.reactivemongo$api$collections$GenericQueryBuilder$$write(genericcollection.scala:323) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
        at reactivemongo.api.collections.GenericQueryBuilder$class.cursor(genericcollection.scala:342) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
        at play.modules.reactivemongo.json.collection.JSONQueryBuilder.cursor(jsoncollection.scala:110) ~[play2-reactivemongo_2.10-0.10.2.jar:0.10.2]
        at reactivemongo.api.collections.GenericQueryBuilder$class.cursor(genericcollection.scala:331) ~[reactivemongo_2.10-0.10.0.jar:0.10.0]
        at play.modules.reactivemongo.json.collection.JSONQueryBuilder.cursor(jsoncollection.scala:110) ~[play2-reactivemongo_2.10-0.10.2.jar:0.10.2]
        at services.common.mongo.MongoDaoComponent$MongoDao$$anon$1.find(MongoDaoComponent.scala:249) ~[classes/:na]
        ... 25 common frames omitted

Any idea? Thanks.

Stennie
  • 63,885
  • 14
  • 149
  • 175
j3d
  • 9,492
  • 22
  • 88
  • 172
  • Can you post the whole stack trace please? There might be more information that can help. – Trisha May 01 '14 at 14:08
  • I've just updated my post with the stack trace I've found in the application log. Anyway, I managed to handle this exception and return the proper error message in the JSON response. – j3d May 01 '14 at 15:14
  • That seems like a weird Exception to throw in the case of invalid input though - is that normal? Might it be better to try converting "53125e9c2004006d04b605abK" into an ObjectId before passing it in to the find() method so you can make sure you're dealing with the correct error case? – Trisha May 01 '14 at 15:44
  • Selector contains valid JSON (not BSON) like {"_id": { "$oid": "53125e9c2004006d04b605abK"}}... then is query.cursor that fails. – j3d May 01 '14 at 17:04

0 Answers0