Can I customize error handling in Lagom?

Can I customize error handling in Lagom?

Yes, the best way to customize error handling in Lagom is to use a custom ExceptionSerializer

This Knoldus blog post explains it well: https://blog.knoldus.com/2018/02/05/exception-serializer-in-lagom/ That example, however, is more focused on how to deserialize exceptions when communicating with external services. Here is an example of using it to serialize an exception to a custom response with status 422 and "text/plain" content-type:public class CustomExceptionSerializer implements ExceptionSerializer {

    private final MessageProtocol MESSAGE_PROTOCOL = new MessageProtocol(
Optional.of("text/plain"),
Optional.of("utf-8"),
Optional.empty()
);

@Override
public RawExceptionMessage serialize(Throwable exception, Collection<MessageProtocol> accept) {
if (exception instanceof DeserializationException) {
return new RawExceptionMessage(
TransportErrorCode.fromHttp(422),
MESSAGE_PROTOCOL,
ByteString.fromString("Could not deserialize request body: " + exception.getMessage())
);
// } else if (exception instanceof ...) {
// handle more types here
} else {
return new RawExceptionMessage(
TransportErrorCode.InternalServerError,
MESSAGE_PROTOCOL,
ByteString.fromString("Exception: " + exception.getMessage())
);
}
}

@Override
public Throwable deserialize(RawExceptionMessage message) {
// Implement this if you also use a Lagom service client to handle errors on the client side
throw new UnsupportedOperationException("not implemented");
}
}

One important thing to be aware of is that this completely overrides the default exception serializer from Lagom, so your custom serializer will need to handle all of the exception types that your service might raise.


    • Related Articles

    • Can Lagom errors be handled using Play error handling?

      No, the Play error handler is not invoked from Lagom, because the Lagom exception serializer intercepts it first. Any exception thrown by application code will be wrapped in a transport exception, serialized to json and a response is built from it. ...
    • Error Handling Best Practices

      The best overview of the error handling options in the standard library we are aware of is tersesystems.com/2012/12/27/error-handling-in-scala, by Will Sargent, a former Lightbender. Digression: One topic he doesn’t cover is why checked exceptions ...
    • Error handling and recovery in Akka Streams

      When developing applications you should assume that there will be unexpected issues. For this, Akka provides a set of supervision strategies to deal with errors within your actors. Akka streams is no different, in fact its error handling strategies ...
    • Possible causes of 'Ask timed out on [...] after [# ms]. Sender[null] sent message of type "com.lightbend.lagom.scaladsl.persistence.CommandEnvelope"'?

      This message means that the actor holding the PersistentEntity didn’t respond on time. It happens when the Future returned by the ask method didn’t complete after #ms. This can be caused by many different reasons. You have to consider it as the ...
    • Does Lagom support multitenancy?

      Our suggestion is to not go down this path. Multi-tenancy was very popular back in the time when people used to deploy one single application on a huge application server. In the era of cloud computing, it's much easier to have different deployments, ...