Scala/Play ClassCastException: Cannot cast java.lang.Integer to java.lang.Long] Ask

351 views
Skip to first unread message

Sarav Ramaswamy

unread,
May 10, 2017, 12:48:20 AM5/10/17
to Play Framework

I have been having this issue for some time where Play will face an "cannot cast an integer to long" exception.

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ClassCastException: Cannot cast java.lang.Integer to java.lang.Long]]

This happens at the line with "msgService.processMsg" call in the below code fragment:

def msgHandler(params: String) = auth.SecuredAction.async { implicit request =>
  usersService.findUserByEmail(request.identity.email) flatMap {
    case Some(ue) =>
      val p = Json.parse(params.toLowerCase())
      val op = (p \ "op").get(0).as[JsString].value
      val prodId = (p \ "prodid").get(0).as[JsString].value
      op match {
        case "get-inventory" =>
          msgService.processMsg(prodId.toLong, ue) flatMap { case res =>
            Future.successful(Redirect(controllers.www.routes.Dashboard.dashboard))
          }
        case _ =>
          Future.successful(Redirect(controllers.www.routes.Dashboard.dashboard))
      }
    case None =>
      Future.successful(Redirect(controllers.www.routes.Dashboard.dashboard))
  }
}

...
def amend(inventory: Inventory): Future[Long] = {
  db.run(inventorys.withFilter(_.id === inventory.id).update(inventory)).mapTo[Long]
} 

def processMsg(prodId: Long, user: UserEntry): Future[Long] = {
  findInventoryByProdIdAndUserId(prodId, user.id) flatMap {
    case Some(inventory) =>
      var updInventory = inventory.copy(status = InventoryStatus.UPDATED)
      Logger.debug(s"Updating inventory: ${updInventory}")
      amend(updInventory)
    case None =>
      throw new Exception("No inventory entry found!!")
  }
}

If I remove the flatMap and keep it like:

msgService.processMsg(prodId.toLong, ue)

then I don't see the error.

Also, if I return Future[Int] instead of Future[Inventory] I don't see the error.

I have used this pattern in few other places in the code but so far haven't been able to isolate what causes this problem. How do I get rid of this error?

Full trace of error:

! @7416eon6i - Internal server error, for (GET) [/processInviteResponse/%7B%22domain%22:%5B%22invites%22%5D,%22jobId%22:%5B%221%22%5D,%22op%22:%5B%22cand-accept%22%5D%7D] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ClassCastException: Cannot cast java.lang.Integer to java.lang.Long]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40)
    at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248)
Caused by: java.lang.ClassCastException: Cannot cast java.lang.Integer to java.lang.Long
    at java.lang.Class.cast(Class.java:3369)
    at scala.concurrent.Future$$anonfun$mapTo$1.apply(Future.scala:405)
    at scala.util.Success$$anonfun$map$1.apply(Try.scala:237)
    at scala.util.Try$.apply(Try.scala:192)
    at scala.util.Success.map(Try.scala:237)
    at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
    at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:235)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
    at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:78)
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@476e0189
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@1141e6d7
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@69368153
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@27b15ff
2017-05-08 21:27:37,452 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@6a1fb0f1
2017-05-08 21:27:37,453 [trace] p.c.s.n.PlayRequestHandler - channelReadComplete: ctx = io.netty.channel.DefaultChannelHandlerContext@3884d6d5

Mariot Chauvin

unread,
May 10, 2017, 2:03:36 AM5/10/17
to play-fr...@googlegroups.com
Hi Sarav,

The problem comes from mapTo[Long] which try to cast an Int as a Long.

Regards,

Mariot



--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/93b2be1c-7acd-4052-9526-630504fca41c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


This e-mail and all attachments are confidential and may also be privileged. If you are not the named recipient, please notify the sender and delete the e-mail and all attachments immediately. Do not disclose the contents to another person. You may not use the information for any purpose, or store, or copy, it in any way.  Guardian News & Media Limited is not liable for any computer viruses or other material transmitted with or as part of this e-mail. You should employ virus checking software.
 
Guardian News & Media Limited is a member of Guardian Media Group plc. Registered Office: PO Box 68164, Kings Place, 90 York Way, London, N1P 2AP.  Registered in England Number 908396


Sarav Ramaswamy

unread,
May 10, 2017, 2:09:01 AM5/10/17
to Play Framework
Hi Mariot,

Thank you so much, that helped. Thought I was trying to get the id of the object being updated but l believe returned Int is number of rows updated.

Sarav
Reply all
Reply to author
Forward
0 new messages