Question

I am using FullAjaxExceptionHandler to handle timeout issue in ajax request. The problem that I am facing is to handle javax.faces.view.facelets.FaceletException. If i have an error in the xhtml page, i dont want to show the stack trace instead show an error page. This i have achieved by specifying error page in web.xml. The problem is that i want to log this error. I am using log4j for other exceptions, but how to write handler for FaceletException. If write another Exception handler is there an order in which i should specify the handler class because i am already using FullAjaxExceptionHandler.

faces-config.xml:

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>
<factory>
    <exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>
</factory>
<render-kit>
    <renderer>
        <component-family>org.primefaces</component-family>
        <renderer-type>org.primefaces.component.ScheduleRenderer</renderer-type>
        <renderer-class>com.example.domain.web.custom.MyScheduleRenderrer</renderer-class>
    </renderer>
</render-kit>
Was it helpful?

Solution

Add a custom Exception handler factory in faces-config.xml

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>
<factory>
    <exception-handler-factory>com.fetchinglife.domain.web.permissions.CustomExceptionHandlerFactory</exception-handler-factory>
</factory>

Create custom exception handler factory

public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory{

private ExceptionHandlerFactory wrapped;

public CustomExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
    this.wrapped = wrapped;
}

@Override
public ExceptionHandler getExceptionHandler() {
    return new CustomExceptionHandler(wrapped.getExceptionHandler());
}

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

}

Create custom exception handler

public class CustomExceptionHandler extends FullAjaxExceptionHandler{

static Logger log = Logger
        .getLogger(CustomExceptionHandler.class);

public CustomExceptionHandler(ExceptionHandler wrapped) {
    super(wrapped);
}

@Override
public void handle() throws FacesException {
    Iterator<ExceptionQueuedEvent> unhandledExceptionQueuedEvents = getUnhandledExceptionQueuedEvents().iterator();
    if(unhandledExceptionQueuedEvents.hasNext()){
        Throwable exception = unhandledExceptionQueuedEvents.next().getContext().getException();
        log.error("FL Error",exception);
    }
    super.handle();
}

}

OTHER TIPS

Unfortunately, it appears that you can't have both.

  • From The JSF Spec, the ExceptionHandlerFactory is expecting a single type to be configured
  • From the source of the FullAjaxExceptionHandler, if the exception is non-ajax, it's rethrown, to be handled by the <error-page> mechanism.

You'll either have to extend or wrap the FullAjaxExceptionHandler to provide both Ajax and Non-Ajax exception handling

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top