Andy C wrote:

> ----- Original Message -----
> From: "Sandy McPherson" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, April 12, 2001 9:06 AM
> Subject: RE: Fed up to the back teeth with tomcat !!!
> 
> 
>> IMHO: before you go blaming something in public you should first identify
>> the problem lies actually with the product you are slagging. Perhaps you
>> should change your database to mysql!
> 
> 
> Yes, I would like to apologise for that.  Please understand last night I was
> extremly irratable and unreasonable after a day of frustration.

I know the feeling...


Here's a concrete suggestion to improve your debugging....

Your users are probably seeing the stack trace you yourself want to see, 
so set up an error page which writes any exceptions to a log file and 
possibly mails the exceptions thrown in the production system to you. In 
each servlet you should do a catch( Throwable ex ) around the servlet 
body and report the exception before re-throwing the it.

I have attached a tag class which you can hack about to acheive the 
desired effect. I have hacked out most of the stuff specific to my 
environment, so it may not work "out of the box". I guess you can figure 
out what should go in the TLD.

The mail is probably a luxury, but it will wake up your operators!

/*
** $Log: MailException.java,v $
** Revision 1.2  2001/02/23 17:20:37  sandy
** Tidy up exception handling
**
** Revision 1.1  2001/02/15 15:17:02  sandy
** Make error page mail exceptions to admin.
**
*/
package com.mapquest.environment;

import java.lang.*;
import java.io.*;
import java.sql.SQLException;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
** Class to mail an exception to the admninistrator
** <p>
** This is a tag which should be used from a JSP error page
** </p>
*/
public class MailException extends TagSupport
{
   
   /**
   ** Constructs a mailable exception
   ** <p>
   ** </p>
   ** @exception The exception whose details should be mailed
   */
   public int doStartTag( )
          throws JspTagException
   {
          try
          {
                 ServletContext application = pageContext.getServletContext( );
                 Throwable exception = (Throwable)pageContext.getAttribute( 
"exception" ); 
                 StringWriter trace = new StringWriter( );
                 PrintWriter traceWriter = new PrintWriter ( trace );
                 exception.printStackTrace( traceWriter );
                 JspWriter out = pageContext.getOut( );
                 System.out.println( trace.toString( ) );
                 try
                 {
                        SQLException dbex = (SQLException)exception;
                        System.out.println( "SQL ANSI 92 State = "+dbex.getSQLState( ) 
);
                        System.out.println( "SQL Error Code = "+dbex.getErrorCode( ) );
                 }
                 catch( java.lang.ClassCastException ex )
                 {
                 }
                 try
                 {
                        // 
                        // see if this is a servlet exception
                        //
                        Throwable rootcause = 
((ServletException)exception).getRootCause( );
                        if ( rootcause != null )
                        {
                           StringWriter roottrace = new StringWriter( );
                           PrintWriter roottraceWriter = new PrintWriter ( roottrace );
                           rootcause.printStackTrace( roottraceWriter );
                           System.out.println( "Root Cause:" );
                           System.out.println( roottrace.toString( ) );
                           SQLException dbex = SQLException)rootcause;
                           System.out.println( "SQL ANSI 92 State = 
"+dbex.getSQLState( ) );
                           System.out.println( "SQL Error Code = "+dbex.getErrorCode( 
) );
                        }
                 }
                 catch( java.lang.ClassCastException ex )
                 {
                 }

                        //
                        // mail the message
                        //
                        String smtphost = application.getInitParameter( 
"MAIL.SMTP.HOST" );
                        if ( smtphost == null )
                        {
                           throw new JspTagException( "MAIL.SMTP.HOST not set");
                        }

                        String to = application.getInitParameter("MAIL.ADMIN.EMAIL");
                        if ( to == null )
                        {
                           throw new JspTagException( "MAIL.ADMIN.EMAIL not set");
                        }
                        //
                        // set up the message
                        //
                        StringWriter message = new StringWriter( );
                        PrintWriter messagewriter = new PrintWriter( message );
                        messagewriter.println( "An exception occurred at "
                                                                   + new 
java.util.Date( ).toString( ) );
                        messagewriter.println( trace.toString( ) );
                        //
                        // set up the email server
                        //
                        Properties props = System.getProperties( );
                        props.put("mail.smtp.host", smtphost );
                        Session mailsession = Session.getInstance( props, null );
                        mailsession.setDebug( false );
                        //
                        // fill in the message
                        //
                        try 
                        {
                           MimeMessage email = new MimeMessage( mailsession );
                           email.setFrom( new InternetAddress( to ) );
                           email.setSubject( "Exception Caught" );
                           email.addRecipient(Message.RecipientType.TO, new 
InternetAddress(to));
                           email.setText( message.toString( ) );
                           //
                           // send message
                           //
                           Transport.send( email );
                        }
                        catch ( MessagingException ex )
                        {
                           System.out.println( ex.toString( ) );
                        }
          }
          catch ( IOException ex )
          {
                 System.out.println( ex.toString( ) );
          }
          return SKIP_BODY;
   }
}

Reply via email to