2

I'm using JSF 2.2 (Mojarra) and Faclets in a web application. I use a custom ExceptionHandler to handle exceptions. I leverage the JSF implicit navigation system and cause the server to navigate to the 'error.xhtml' page.

public class FrontEndExceptionHandler extends ExceptionHandlerWrapper {

private ExceptionHandler wrapped;

FrontEndExceptionHandler(ExceptionHandler exception) {
    this.wrapped = exception;
}

@Override
public ExceptionHandler getWrapped() {
    return wrapped;
}

@Override
public void handle() throws FacesException {
    final Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator();
    while (iter.hasNext()) {
        ExceptionQueuedEvent event = iter.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();

        // get the exception from context
        Throwable t = context.getException();

        final FacesContext fc = FacesContext.getCurrentInstance();
        final Flash flash = fc.getExternalContext().getFlash();
        final NavigationHandler nav = fc.getApplication().getNavigationHandler();

        try {

            // redirect error page
            flash.put("erorrDetails", t.getMessage());
            nav.handleNavigation(fc, null, "/errors/error.xhtml");
            fc.renderResponse();
        } finally {
            // remove it from queue
            iter.remove();
        }
    }
    // parent handle
    getWrapped().handle();
}

}

This assumes that the exception to be handling is not happening during the Render Response phase. But the exception occurs within an Facelets page also during the Render Response phase.Therefore following code doesn't work correct:

nav.handleNavigation(fc, null, "/errors/error.xhtml");

Does anybody has a Idea how to convey the desired information? Is there a way to navigate to the error.xhtml without using NavigationHandler?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    This is not trivial. It boils down to "build and render the view yourself". Does related question this answer your question? http://stackoverflow.com/q/18410007 – BalusC May 24 '16 at 14:45

0 Answers0