3 Replies Latest reply on Aug 3, 2010 8:10 AM by cash1981

    Manager Exception with page.xml

    daniele4
      In my application I managed some exeception with page.xml. When I got some exception i redirect to a error page and i'd like do send a email or write a log. So i added an action to the error page; it's work but how can i get the exception stack trace to write it on the email body?

      this is the code

      <exception class="javax.ejb.NoSuchEJBException">
        <redirect view-id="/error.xhtml">
           <message severity="warn">"My message"</message>
        </redirect>
      </exception>

      <page view-id="/error.xhtml" login-required="true" action="#{email.send()}" >
      </page>


        • 1. Re: Manager Exception with page.xml
          cash1981

          The correct way is to extends Seam's ExceptionHandler.


          From this class you can get the stacktrace, and call an event that will either log or send the stacktrace as email.


          ExceptionHandler


          Pseudo code:



          @Name("org.jboss.seam.exception.ExcetionHandler")
          @Transactional
          @Bypassinterceptors
          public class MyExceptionHandler extends ExceptionHandler {
          
              public void handle(Exception e) {
                 StringWriter writer = new StringWriter();
                 e.printStacktrace(writer);
          
                 Events.instance().raiseEvent("ExceptionOccurred", writer.toString());
                 //This event will trigger an email to be sendt with the stacktrace
                 
              }
          }



          The above code wont work as is, you will need to rewrite it slightly

          • 2. Re: Manager Exception with page.xml
            daniele4

            good solution!!


            thanks

            • 3. Re: Manager Exception with page.xml
              cash1981

              Ok so this is how we do it, you can basically copy/paste this




              import java.io.PrintWriter;
              import java.io.StringWriter;
              import org.jboss.seam.Component;
              import org.jboss.seam.ScopeType;
              import org.jboss.seam.annotations.Install;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.Scope;
              import org.jboss.seam.annotations.intercept.BypassInterceptors;
              import org.jboss.seam.core.Events;
              import org.jboss.seam.exception.Exceptions;
              
              @Name("org.jboss.seam.exception.exceptions")
              @Scope(ScopeType.APPLICATION)
              @Install(precedence = Install.APPLICATION)
              @BypassInterceptors
              public class ExceptionHandler extends Exceptions {
                   
                   public void handle(Exception ex) throws Exception {
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        ex.printStackTrace(pw); //Put the stracktrace in StringWriter
              
                        Events.instance().raiseAsynchronousEvent(SystemlogListener.EVENT_ADD_DETAILED_SYSTEMLOG, ex.getMessage()),sw.toString());               
                        
                        super.handle(ex);
                   
                   }
              }