How to customize JAX-RS application exception handling?
Hello,
I have configured a simple JAX-RS (not WebEngine as it is for programmatic web services) application as described in the documentation. I am using the
Nuxeo-WebModule: org.nuxeo.ecm.webengine.jaxrs.scan.DynamicApplicationFactory
line to discover my JAX-RS classes.
Everything is working as I need, with the small detail that the text/plain error response for WebExceptions that are raised in my JAX-RS classes includes the stack trace. I want them to only include the status line with an empty body.
What is the correct way to avoid/modify this behavior? I tried putting an ExceptionMapper implementation in my JAX-RS package but the DynamicApplicationFactory does not seem to discover/apply it. It is annotated with @Provider and is in the correct package.
Thanks.
You can override the handleError() method in your ModuleRoot class. Here is a sample
@Override
public Object handleError(WebApplicationException e) {
if (e instanceof WebSecurityException) {
return Response.status(401).entity(
getTemplate("error/error_401.ftl")).type("text/html").build();
} else if (e instanceof WebResourceNotFoundException) {
return Response.status(404).entity(
getTemplate("error/error_404.ftl")).type("text/html").build();
} else {
return super.handleError(e);
}
}
DynamicApplicationFactory
is doing the correct thing with respect to making those available.
I basically stopped reading before the line "Declaring a WebEngine Application in Nuxeo" since I was only interested in a JAX-RS application.
Will I need to subclass ModuleRoot
to customize this behavior?
I currently have a workaround which involves using a ContainerResponseFilter to remove the body from error responses, but clearly this is worse than stopping the stack trace from being printed in the first place.