Exception handling in JSF ajax requests
Asked Answered
K

1

10

How do I handle the exception and access the stack trace when an exception is thrown while processing a JSF ajax request? Right now, I only get the exception class name and message in a JavaScript alert when JSF project stage is set to Development. Even worse, there's no visual feedback whatsoever when JSF project stage is set to Production, and the server log doesn't show any information about the exception.

If that's relevant, I'm using GlassFish in Netbeans.

Krefeld answered 17/12, 2014 at 13:31 Comment(3)
Do you want to display full exception stack-trace on web pages during asynchronous requests?Fess
No. I have a <h:commandButton> on the page which when pressed (processed using ajax) is resulting in an exception. (i.imgur.com/PdgQvP9.png) but the Glassfish output in Netbeans doesn't show the stack trace. (i.imgur.com/a7jqUVz.png). On the other hand, if it is a normal http request, the stack trace is displayed in the HTML output (i.imgur.com/EgxHSP4.png) and the Glassfish server output in Netbeans shows the stack trace (i.imgur.com/6izzfPs.png). cont....Krefeld
cont... I want the stack trace to be displayed in the Glassfish output of Netbeans (like this:i.imgur.com/6izzfPs.png ) when an error occurs during an ajax request. @FessKrefeld
W
23

This problem is known and fleshed out in among others the OmniFaces FullAjaxExceptionHandler showcase.

By default, when an exception occurs during a JSF ajax request, the enduser would not get any form of feedback if the action was successfully performed or not. In Mojarra, only when the project stage is set to Development, the enduser would see a bare JavaScript alert with only the exception type and message.

The technical reason is that asynchronous requests (read: Ajax requests) by default don't return a synchronous response (read: a full page). Instead, they return small instructions and parts how to update the HTML DOM tree of the already-opened page. When an exception occurs, then these instructions are basically fully absent. Instead, some error information is sent back. You can usually handle them in the onerror attribute of the Ajax component and e.g. display an alert or perhaps perform a window.location change. At least, this is what JSF expected from you.

In order to catch and log the exception and optionally change the whole response, you basically need to create a custom ExceptionHandler. Standard JSF unfortunately doesn't provide a default one out the box (at least, not a sensible one). In your custom exception handler you will be able to get hands on the Exception instance causing all the trouble.

Here's a kickoff example:

public class YourExceptionHandler extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;

    public YourExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public void handle() throws FacesException {
        FacesContext facesContext = FacesContext.getCurrentInstance();

        for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
            Throwable exception = iter.next().getContext().getException(); // There it is!

            // Now do your thing with it. This example implementation merely prints the stack trace.
            exception.printStackTrace();

            // You could redirect to an error page (bad practice).
            // Or you could render a full error page (as OmniFaces does).
            // Or you could show a FATAL faces message.
            // Or you could trigger an oncomplete script.
            // etc..
        }

        getWrapped().handle();
    }

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

}

In order to get it to run, create a custom ExceptionHandlerFactory as follows:

public class YourExceptionHandlerFactory extends ExceptionHandlerFactory {

    private ExceptionHandlerFactory parent;

    public YourExceptionHandlerFactory(ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        return new YourExceptionHandler(parent.getExceptionHandler());
    }

}

Which needs to be registered in faces-config.xml as follows:

<factory>
    <exception-handler-factory>com.example.YourExceptionHandlerFactory</exception-handler-factory>
</factory>

Alternatively, you can go ahead using the OmniFaces one. It will fully transparently make sure that exceptions during asynchronous requests behave the same as exceptions during synchronous requests, using <error-page> configuration in web.xml.

See also:

Wyant answered 17/12, 2014 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.