0 Replies Latest reply on Jun 6, 2007 9:46 AM by gonzalad

    Customizing error handling

    gonzalad Apprentice

      Hello,

      I would like to implement a generic error handling mecanism with Seam in order to catch and log exceptions generated from whatever the Jsf phase.

      After having a look at Seam source code, I have found a - limited - solution for this, but before going on :
      1. is there's a better way to do it.
      2. if this is the way to go would it make some sens to create protected accessors (getters/setters) to exceptionHandlers attribute
      of Seam Exceptions class in order to be able to access this list from my child class ?
      3. know how I can catch ALL jsf errors.

      Since I want to build a generic error handling mecanism, I don't want to use pages.xml [1]

      For question 1 & 2 here's a sample code (didn't try to run or compile it - just for the idea) :
      In order to plugin my generic error handling, I'll create my own org.jboss.seam.core.exceptions component which will inherit from
      org.jboss.seam.core.Exceptions.

      @Scope(ScopeType.APPLICATION)
      @Intercept(NEVER)
      @Install(precedence=FRAMEWORK)
      @Name("org.jboss.seam.core.exceptions")
      public class Exceptions extends org.jboss.seam.core.Exceptions
      {
      
       /**
       * <p>Need to redeclare this list since it's declared private on parent class.</p>
       */
       protected List<ExceptionHandler> myExceptionHandlers = new ArrayList<ExceptionHandler>();
      
       public void handle(Exception e) throws Exception
       {
       //build a list of the nested exceptions
       List<Exception> causes = new ArrayList<Exception>();
       for (Exception cause=e; cause!=null; cause=EJB.getCause(cause))
       {
       causes.add(cause);
       }
       //try to match each handler in turn
       for (ExceptionHandler eh: exceptionHandlers)
       {
       //Try to handle most-nested exception before least-nested
       for (int i=causes.size()-1; i>=0; i--)
       {
       Exception cause = causes.get(i);
       if ( eh.isHandler(cause) )
       {
       eh.handle(cause);
       return;
       }
       }
       }
      
       //finally, call parent Exceptions handle
       super.handle (e);
       }
      
       @Create
       public void initialize() throws Exception
       {
       super.initialize();
       ExceptionHandler anyhandler1 = MyExceptionHandler();
       myExceptionHandlers.add( new AnnotationRedirectHandler() );
       }
      }


      For question 3, I've made a few tests, Seam exception handling intercepts errors throw during INVOKE_APPLICATION_PHASE
      and RENDER_VIEW_PHASE.
      Errors during UPDATE_MODEL_VALUES phase are not handled (i.e. when throwing an nuchecked exception from a managed bean setter).
      Don't know the handling for RESTORE_VIEW(1), APPLY_REQUEST_VALUES(2), PROCESS_VALIDATIONS(3).
      Is there a way to handle all those errors ?

      Thank you very much for your help

      [1] : The main purpose of this error handling is to systematically log errors. I'll have a publish / subscribe mechanism used for other applications
      (i.e. batch application), and want to reuse it for Seam applications)
      I don't want to use pages.xml for that, since all applications I'm going to build will use this same
      error logging mechanism. Application will use pages.xml to handle errors i.e. to navigate to an error page.