2 Replies Latest reply on May 13, 2010 1:10 AM by Walter White

    Exception Handling and Logging

    Walter White Novice

      Hi all,


      I have a exception observer class which is supposed to catch all exception events.  When it catches such an event, it notifies administrators of the exception.  My question is, is it possible to have access to the URL or Identity if the exception occurred as a direct result from page navigation?  That would make my life a little bit easier when tracking down bugs.



      This is the class that I have setup to be an interceptor or listener rather.  I am thinking that I might be able to do something with the context to get the page and possibly the identity of the logged in user from the session context.


      package com.walterjwhite.interceptor;
      
      import java.util.Collection;
      import java.util.Date;
      
      import javax.ejb.Stateless;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Observer;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.contexts.Contexts;
      import org.jboss.seam.faces.Renderer;
      import org.jboss.seam.framework.HibernateEntityQuery;
      import org.jboss.seam.log.Log;
      
      import com.walterjwhite.enumeration.EmailType;
      import com.walterjwhite.model.Member;
      
      @Stateless
      @Name("asynchronousExceptionInterceptor")
      public class AsynchronousExceptionInterceptor
      {
              @Logger
              protected transient Log log;
      
              @In
              protected Exception cause;
      
              @In
              protected HibernateEntityQuery<Member> administratorQuery;
      
              @In
              protected Renderer renderer;
      
              @Out
              protected Collection<Member> administrators;
      
              @Out
              protected String subject;
      
              @Out
              protected Date dateOccurred;
      
              @Out
              protected String stackTrace;
      
              @Observer(
              { "org.jboss.seam.async.asynchronousExceptionHandler", "org.jboss.seam.exceptionHandled", "org.jboss.seam.exceptionNotHandled" })
              public void onError()
              {
                      log.info("notifying administrators of exception.");
                      administrators = administratorQuery.getResultList();
      
                      dateOccurred = new Date();
                      
                      Contexts.getSessionContext();
      
                      subject = cause.getMessage();
                      StringBuilder buffer = new StringBuilder();
                      for (StackTraceElement element : cause.getStackTrace())
                              buffer.append(element.toString() + "\n");
      
                      stackTrace = buffer.toString();
                      buffer = null;
      
                      renderer.render(EmailType.EXCEPTION.getPath());
      
                      log.error("captured exception", cause);
                      log.info("notified administrators of exception.");
              }
      }



      Is this possible?


      I do not have any integration tests running yet, but I'm trying to understand from a high level how things can work.



      Thanks,
      Walter

        • 1. Re: Exception Handling and Logging
          virendra agarwal Newbie

          hey Walter,
          Hey i have the same issue that you have already implemented i need to catch all exception what ever it is end need to send mail to admin whenever exception occurs.
          Can you tell me how can i use your code to achieve this.
          Where i have to put entries for this class so that seam can it can be called when ever any exception occurs.
          Please help me i am really stuck with issue.


          Thanks
          Viren

          • 2. Re: Exception Handling and Logging
            Walter White Novice

            Viren,


            Strange I didn't get an email from Seam, but I got the one from my website.


            Well, what I did is override the default exception handler to call my handler and pass all exceptions through it:




            org.jboss.seam.async.AsynchronousExceptionHandler
            
            @Override
                 public void handleException(Exception throwable)
                 {






            You want to override that method and call your exception handler in it.


            Then in your exception handler, you can call a query to find all the admins or whatever you need to do to determine who to send it out to and then send the email.


            I am developing these modules further and was planning to open source them and get them included with Seam, but that won't happen until Seam 3 is GA.  They provide several routing options, email, xmpp, sms, etc.  Until then, this should suffice for you in catching all the exceptions.


            An important thing to note that, this will catch all exceptions, asynchronous and the ones thrown in an Http Request.  I believe these are handled in the same thread, so you have access to the current Faces Context (http request ...).  That is really useful for debugging purposes.



            Let me know if that helps.


            Walter