Search code examples
javaexceptionspark-java

How to handle exceptions for multiple Route


I'm getting to grips with the Spark Framework and I'm trying to understand the best way of handling exceptions in a uniform way for multiple Routes.

At the moment I have a number of Routes which all handle exceptions along the lines of:

...
catch (final Exception e) {
    ...
    response.status(418);
    return e.getMessage();
}
...

This leaves a lot to be desired, mainly the exception logic is duplicated between them. I know that it can be improved by refactoring but I was wondering if there's something similar to the ExceptionHandler mechanism in Spring where you can perform an action when a particular exception is thrown, e.g.:

@ExceptionHandler(Exception.class)
public void handleException(final Exception e, final HttpServletRequest request) {
    ...executed for the matching exception...
}

So, is there a Spark-esque mechanism for exception handling? I've checked the documentation and come up short. If there isn't, I'll go ahead with my refactoring plans. Thanks.


Solution

  • You can handle exceptions like so:

    get("/throwexception", (request, response) -> {
        throw new NotFoundException();
    });
    
    exception(NotFoundException.class, (e, request, response) -> {
        response.status(404);
        response.body("Resource not found");
    });
    

    Example taken from the Spark docs.