This gist has been updated (see history for previous discussions) and aims to discuss solutions to AEROGEAR-426 "General/fall-back error page on Controller".
aerogear-controller branch
aerogear-controller-demo branch
Example of specifying a single exception:
@Override
public void configuration() throws Exception {
route()
.on(DeloreanException.class)
.to(ExceptionHandler.class).errorPage();
route()
.from("/")
.on(RequestMethod.GET)
.to(Home.class).index();
}
Example of specifying multiple exceptions:
@Override
public void configuration() throws Exception {
route()
.on(DeloreanException.class, SomeOtherException.class)
.to(ExceptionHandler.class).errorPage();
route()
.from("/")
.on(RequestMethod.GET)
.to(Home.class).index();
}
It's possible for the errorPage to take an Exception as a parameter, for example if some action like logging should take place:
@Override
public void configuration() throws Exception {
route()
.on(DeloreanException.class, SomeOtherException.class)
.to(ExceptionHandler.class).errorPage(param(Exception.class));
route()
.from("/")
.on(RequestMethod.GET)
.to(Home.class).index();
}
In the above examples the errorPage would be resolved to WEB-INF/ExceptionHandler/errorPage.jsp.
To support multiple exceptions the on method of RouteBuilder looks like this:
TargetEndpoint on(Class<? extends Throwable> exception, Class<?>... exceptions);
Now, this is somewhat ugly and the second parameter is not typesafe (though runtime check is performed) but was the cleanest solution I could think of with regard to ease of use, and without polluting the API.
The issue here was to avoid compiler warnings about using varargs in combination with generics (Class) from client code (with @Safevarargs in Java7 which should be able to make this typesafe at compile time). Hopefully, with the first parameter being typed this will make it understandable for users and obvious that the classes following the first parameter should also be of the same type.
What are your thoughts on this?
@qmx @abstractj I've taken another shot at this and hopefully this is a littler closer to the goal.
As discussed in the earlier comments a catch-all exception route could be added like this:
In this case the Home class would have a method named error which takes an exception as a parameter.
The parameter would be optional and perhaps we could support passing an Object[] of the params sent with the original request (if any that is)?
DefaultRoute will catch any exception and see if there is a route that can handle the exception in question:
The above would then forward the result, if any, from the error method to the resolved view. The exception is set as an attribute in the forwardError method and could be accessed in jsp page like this:
What are your thoughts on this?