views:

105

answers:

2

Hello, I'm using Richfaces 3.2.2 and need to show the user the 500 error page when there is Exception. The issue is that when I use ajax event I can't show the user the 500 error when there is an Exception. I've already defined the error page on web.xml.

Excuse My English. Any suggestion please ?

+1  A: 

Since you are using probably JSF1.2 and not JSF2, you can use FaceletViewHandler to handle the exceptions.

public class CustomViewHandler extends FaceletViewHandler {
    ...
    @Override
    protected void handleRenderException(FacesContext context, Exception ex) throws IOException, ELException,
        FacesException {
        try {
            ..

            getSessionMap().put("GLOBAL_ERROR", ex);
            getHttpResponseObject().sendRedirect("/error.jsf");
        } catch (IOException e) {
            log.fatal("Couldn't redirect to error page", e);
        }
    }
}

of course, you need to handle it in the bean, just extract the exception from session:

Throwable ex = (Exception) getSessionMap().remove("GLOBAL_ERROR");
Odelya
Thank you, Your solution is working.
imrabti
you are welcome. please mark my answer with V (green) below the 1 number
Odelya
+2  A: 

Check the RichFaces developer guide chapter 5.10.1.

5.10.1 Request Errors Handling

To execute your own code on the client in case of an error during Ajax request, it's necessary to redefine the standard "A4J.AJAX.onError" method:

A4J.AJAX.onError = function(req, status, message){
    window.alert("Custom onError handler "+message);
}

The function defined this way accepts as parameters:

  • req - a params string of a request that calls an error
  • status - the number of an error returned by the server
  • message - a default message for the given error

Thus, it's possible to create your own handler that is called on timeouts, internal server errors, and etc.

So, to display the server-generated error response, you'd like to do the following:

A4J.AJAX.onError = function(req, status, message){
    document.open();
    document.write(req.responseText);
    document.close();
}

To redirect to the error page, do as follows:

A4J.AJAX.onError = function(req, status, message){
    window.location = 'error.jsf';
}

You'll only need to pass mandatory error details as request parameter or let the server side store it in the session as Odelya suggested.

Related question:

BalusC