dev@glassfish.java.net

Re: using NavigationHandler in ExceptionHandlerWrapper

From: Derek Knapp <derek_at_itracmedia.com>
Date: Thu, 04 Aug 2011 16:17:14 -0400

I see, the problem is when the exception is thrown in the PostConstruct,
if its in a button's action, it works properly..

Is there any way around this problem?


Derek


On 11-08-04 03:57 PM, Derek Knapp wrote:
> Why will the following code not redirect to my error page? I have been
> Googling forever, with no luck :(
>
>
>
>
> package com.itrac.exception;
>
> import java.util.Iterator;
> import javax.faces.FacesException;
> import javax.faces.application.NavigationHandler;
> import javax.faces.context.ExceptionHandler;
> import javax.faces.context.ExceptionHandlerWrapper;
> import javax.faces.context.FacesContext;
> import javax.faces.event.ExceptionQueuedEvent;
> import javax.faces.event.ExceptionQueuedEventContext;
>
> /**
> *
> * @author derek
> */
> public class ItracExceptionHandler extends ExceptionHandlerWrapper
> {
> private ExceptionHandler wrapped;
>
> public ItracExceptionHandler(ExceptionHandler wrapped)
> {
> this.wrapped = wrapped;
> }
>
> @Override
> public ExceptionHandler getWrapped()
> {
> return wrapped;
> }
>
> @Override
> public void handle() throws FacesException
> {
> for (Iterator<ExceptionQueuedEvent> i =
> getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();)
> {
> ExceptionQueuedEvent event = i.next();
> ExceptionQueuedEventContext context =
> (ExceptionQueuedEventContext) event.getSource();
> Throwable t = context.getException();
>
> // redirect to error page
> FacesContext fc = FacesContext.getCurrentInstance();
> NavigationHandler nav =
> fc.getApplication().getNavigationHandler();
> nav.handleNavigation(fc, null, "/error");
> fc.renderResponse();
>
> // remove it if you processed it
> i.remove();
> }
>
> // let the next ExceptionHandler(s) deal with the others
> getWrapped().handle();
> }
> }
>