I’ve recently taken a lot of inspiration from a blog post titled Akka and Spray over at the Eigengo blog. It covers a lot of ground and comes highly recommended if you are learning Scala, Akka & Spray. The only thing I didn’t like was how it handled marshalling of errors, and I intend to show a better way here.
Imagine an endpoint to register a user. It should either return the user or a failure. If the user already exist, we want to return a 400 error, otherwise a 500 will do. Here’s my exceptionHandler:
I initially used
mapTo[Try[User]] in my service:
This works, however, I don’t like that I have to wrap responses from the register actor in Success/Failure. And I particularly don’t like that I have to return an exception in the failure case:
You cannot simply ditch the
Failure wrappers and
mapTo[User] because if you don’t return a user you will just get a
ClassCastException rather than anything useful you can handle in your exceptionHandler. However, I found I could use
collect on the future to get the effect I want:
Now, in my register actor, I don’t have to wrap my values and am free to do this:
It feels right to me that the register handler should not have to know that its callers will be using the ask pattern, and that the service layer should chose the exceptions to throw in response to failure messages. I would argue it makes the
register actor more reusable from non-REST contexts.