As described in BalusC's and A. Tijms' book "The Definitive Guide to JSF in Java EE 8" (and similarly coded in Omnifaces) I have constructed the following CustomException handler for exceptions occuring in regular and ajax requests.
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
public CustomExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
super(wrapped);
}
@Override
public ExceptionHandler getExceptionHandler() {
return new CustomExceptionHandler(getWrapped().getExceptionHandler());
}
private class CustomExceptionHandler extends ExceptionHandlerWrapper {
private CustomExceptionHandler(ExceptionHandler wrapped) {
super(wrapped);
}
@Override
public void handle() throws FacesException {
handleException(FacesContext.getCurrentInstance());
getWrapped().handle();
}
private void handleException(FacesContext fctx) {
Iterator<ExceptionQueuedEvent> it
= getUnhandledExceptionQueuedEvents().iterator();
if (fctx == null
|| fctx.getExternalContext().isResponseCommitted()
|| !it.hasNext()) {
return;
}
Throwable t = it.next().getContext().getException();
Throwable tc = t;
while ((tc instanceof FacesException || tc instanceof ELException)
&& tc.getCause() != null) {
tc = tc.getCause();
}
renderErrorPageView(fctx, t);
it.remove();
while (it.hasNext()) {
it.next();
it.remove();
}
}
private void renderErrorPageView(FacesContext fctx, Throwable t) {
ExternalContext ctx = fctx.getExternalContext();
String uri = ctx.getRequestContextPath()
+ ctx.getRequestServletPath();
Map<String, Object> requestMap = ctx.getRequestMap();
requestMap.put(RequestDispatcher.ERROR_REQUEST_URI, uri);
requestMap.put(RequestDispatcher.ERROR_EXCEPTION, t);
String viewId = "/view/stop_error.xhtml";
Application app = fctx.getApplication();
ViewHandler viewHandler = app.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(fctx, viewId);
fctx.setViewRoot(viewRoot);
try {
ctx.responseReset();
if (!fctx.getPartialViewContext().isAjaxRequest()) {
ctx.setResponseStatus(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
ViewDeclarationLanguage vdl
= viewHandler.getViewDeclarationLanguage(fctx, viewId);
vdl.buildView(fctx, viewRoot);
fctx.getPartialViewContext().setRenderAll(true);
vdl.renderView(fctx, viewRoot);
fctx.responseComplete();
}
catch (IOException e) {
throw new FacesException(e);
}
finally {
requestMap.remove(RequestDispatcher.ERROR_EXCEPTION);
}
}
}
}
In web.xml I have
<error-page>
<exception-type>javax.faces.application.ViewExpredException</exception-type>
<location>/faces/view/expired.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/view/stop_error.xhtml</location>
</error-page>
For regular requests it works like a charm. However, if there is a (Runtime)Exception in an ajax request, I only get the java script alert box that the asynchronous request returned nothing (in Development mode) or nothing (in Production mode). The above code runs fully through, however, the error page is not displayed. I am using Tomcat 9 and Mojarra 2.3.8 under Java 11. What I am doing wrong?
I tested using the composite component described in http://balusc.omnifaces.org/2013/01/composite-component-with-multiple-input.html where I throw an IllegalStateException within the updateDaysIfNecessary method which is triggered by changing the month in the repective drop down box.