How it works...
In this recipe, you learned to handle exceptions in routes to better customize the behavior of your application. This is achieved thanks to ExceptionHandler. ExceptionHandler expects PartialFunction[Throwable, Route] where you define what to do for each Throwable case. In our case, we returned a 400 response in the case of ArithmeticException and a 503 response in the case of AskTimeoutException. ArithmeticException is thrown when you call the URI /divide with parameter b set to 0. AskTimeoutException is thrown when you call the URI /futureTimingOut since this endpoint expects a message back from the actor; however, your actor ignores all the messages, and therefore, does not respond. This is only for demonstration purposes; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access