scala exception serialization

430 views
Skip to first unread message

Tim Pigden

unread,
Jan 28, 2017, 12:13:41 PM1/28/17
to Lagom Framework Users
Is there an example of how this fits together?

I've got a 
case class ClientAlreadyExistsException(clientId: ClientId) extends TransportException(TransportErrorCode.UnsupportedData, s"client $clientId already exists")

but in my service test I get

com.lightbend.lagom.scaladsl.api.transport.DeserializationException: client tim already exists (TransportErrorCodeImpl(400,1003,Unsupported Data/Bad Request)) was not an instance of optrak.lagomtest.model.impl.ClientEntity$ClientAlreadyExistsException, but an instance of com.lightbend.lagom.scaladsl.api.transport.DeserializationException
ScalaTestFailureLocation: optrak.lagomtest.model.impl.ClientServiceScalaTest$$anonfun$1$$anonfun$apply$mcV$sp$2$$anonfun$apply$3 at (ClientServiceScalaTest.scala:58)
org.scalatest.exceptions.TestFailedException: com.lightbend.lagom.scaladsl.api.transport.DeserializationException: client tim already exists (TransportErrorCodeImpl(400,1003,Unsupported Data/Bad Request)) was not an instance of optrak.lagomtest.model.impl.ClientEntity$ClientAlreadyExistsException, but an instance of com.lightbend.lagom.scaladsl.api.transport.DeserializationException

Looking at 

it's not clear as to how these things are actually supposed to be used.
Online-Auction throws a couple of exceptions but it doesn't attempt to serialize them as far as I can see.

James Roper

unread,
Jan 28, 2017, 12:43:45 PM1/28/17
to Tim Pigden, Lagom Framework Users
I thought this was documented but it turns out it's not.

Lagom can serialize any exceptions that extend TransportException, but it can only deserialize them back to the original exception if it knows about them, otherwise it will deserialize to a best fit type based on the status code.  The exceptions that it knows about are hard coded, you can see the logic for selecting an exception here:


First it checks the exception name to see if it has an exception with that name - in the case of a custom exception it won't.  Then it checks by HTTP status code, and in your case it's finding the DeserializationException because you're using a 400 error.

If you want to be able to serializer/deserialize custom exceptions, you need to write your own exception serializer.  Here's the default one to see an example:

https://github.com/lagom/lagom/blob/master/service/scaladsl/api/src/main/scala/com/lightbend/lagom/scaladsl/api/deser/ExceptionSerializer.scala#L54

What we should probably do in Lagom is provide an abstract class that handles the json serialization of TransportException for you, but allows you to easily plug in your own exception types.  For now it would probably suffice for you to just copy that class, and do lookups on your own exception types before doing the lookups on the built in ones.

Cheers,

James

--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/34ff998d-8af4-44c1-9ee3-c586de192cec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
James Roper
Software Engineer

Lightbend – Build reactive apps!
Twitter: @jroper

James Roper

unread,
Jan 28, 2017, 1:38:29 PM1/28/17
to Tim Pigden, Lagom Framework Users
See https://github.com/lagom/lagom/pull/419 for better documentation and a simple way to plug in deserialization of custom transport exceptions.
Reply all
Reply to author
Forward
0 new messages