JSF 2: Is this a good approach to handling business exceptions?

Following my previous question in Creating FacesMessage in an action method outside of the JSF transform / validation mechanism? I am trying to handle exceptions emanating from a business layer outside of my managed beans.

The strategy is to find and translate business exceptions into entity messages in PhaseListener,

It works the way I expected, but they just wonder if it just invents the wheel or does it wrong with the wrong way?

Here is my code snippet:

public class BusinessExceptionHandler implements PhaseListener {

    @Override
    public void afterPhase(PhaseEvent phaseEvent) {
        ExceptionHandler exceptionHandler = phaseEvent.getFacesContext().getExceptionHandler();

        // just debugging the handled exception, nothing here
        /*
        for (ExceptionQueuedEvent event : exceptionHandler.getHandledExceptionQueuedEvents()) {
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
            System.out.println("handled exception : " + context.getException());
        }*/

        for (Iterator<ExceptionQueuedEvent> it = exceptionHandler.getUnhandledExceptionQueuedEvents().iterator(); 
                it.hasNext();) {

            ExceptionQueuedEvent event = it.next();
            ExceptionQueuedEventContext eventContext = (ExceptionQueuedEventContext) event.getSource();
            Throwable e = eventContext.getException();
            System.out.println("unhandled exception : " + e);

            // get the root cause exception
            while (e.getCause() != null) { 
                e = e.getCause();
            }

            System.out.println("cause exception : " + e + 
                ", cause exception is BE : " + (e instanceof BusinessException));

            // handle BE
            if (e instanceof BusinessException) {
                BusinessException be = (BusinessException) e;
                System.out.println("processing BE " + be);
                FacesMessage message = Messages.getMessage(
                    "com.corejsf.errors", 
                    be.getMessage(), 
                    be.getParamValues()
                );
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, message);
                it.remove(); // remove the exception

                // works fine without this block, if BE is thrown, always return to the original page
                /*
                NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
                System.out.println("navigating to " + context.getViewRoot().getViewId());
                navigationHandler.handleNavigation(context, context.getViewRoot().getViewId(), null);
                */
            }
        }
    }

    @Override
    public void beforePhase(PhaseEvent phaseEvent) {
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.INVOKE_APPLICATION;
    }

}

Thank!

Regards, Albert Cam

+5
source share
2 answers

- JSF 1.x JSF 2.x. JSF 2.x, JSF 2.x PhaseListener ExceptionHandler API.

, ViewExpiredException 282 "JSF 2.0: ", ( 1- ).

+2

All Articles