You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@apache.org on 2001/01/07 01:17:07 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/modules/generators ErrorHandler.java

costin      01/01/06 16:17:06

  Modified:    src/share/org/apache/tomcat/modules/generators
                        ErrorHandler.java
  Log:
  - Added showDebugInfo property to the ErrorHandler module - it'll act as default
  ( explicit properties are easier to document, and it's better to set the properties
  on the module if it's specific to the module )
  
  - Bug fix: the buffer will be reset before displaying the exception, even for
  the default handler. It shouldn't be any distinction between the "default" error
  handler and a user-defined handler, and this helps if the buffer may have
  garbage ( open tags, etc ).
  
  - Bug fix: reset the exception state before calling the error servlet. This prevent
  the display of "error in the error handler"
  
  - Bug fix: if( ! (ex instanceof ... ) -> if( ex!=null && ! ( ex instanceof ... )
  
  - Bug fix: add the <head>, <body>, etc only if the buffere is "clean". The previous
  test ( getBytesWritten() ) was bad, the correct test is "isNew()" ( because both
  chars and bytes can be written to the buffer ).
  
  Revision  Changes    Path
  1.3       +43 -19    jakarta-tomcat/src/share/org/apache/tomcat/modules/generators/ErrorHandler.java
  
  Index: ErrorHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/generators/ErrorHandler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ErrorHandler.java	2001/01/01 02:07:24	1.2
  +++ ErrorHandler.java	2001/01/07 00:17:06	1.3
  @@ -77,10 +77,15 @@
    */
   public final class ErrorHandler extends BaseInterceptor {
       private Context rootContext=null;
  +    boolean showDebugInfo=true;
       
       public ErrorHandler() {
       }
   
  +    public void setShowDebugInfo( boolean b ) {
  +	showDebugInfo=b;
  +    }
  +
       public void engineInit( ContextManager cm )
       	throws TomcatException
       {
  @@ -98,7 +103,7 @@
       {
   	if( ctx.getHost() == null && ctx.getPath().equals(""))
   	    rootContext = ctx;
  -	boolean showDebugInfo=true;
  +
   	ContextManager cm=ctx.getContextManager();
   	String dI=cm.getProperty( "showDebugInfo" );
   	if( dI!=null && ( dI.equalsIgnoreCase("no") ||
  @@ -185,7 +190,12 @@
   	    return;
   	}
   
  -	if (!isDefaultHandler && !res.isBufferCommitted())
  +	// XXX The original code didn't reset the buffer if
  +	// isDefaultHandler :	if (!isDefaultHandler && ...
  +	// Is there any reason for that ?
  +	// I also think we should reset the buffer anyway, to get
  +	// in a stable state - even if the buffer is commited
  +	if ( !res.isBufferCommitted())
   	    res.resetBuffer();
   
   	req.setAttribute("javax.servlet.error.status_code",new Integer( code));
  @@ -194,6 +204,8 @@
   	if( debug>0 )
   	    ctx.log( "Handler " + errorServlet + " " + errorPath);
   
  +	// reset error exception
  +	res.setErrorException( null );
   	errorServlet.service( req, res );
   	Exception ex=res.getErrorException();
   	if( ex!=null && ! (ex instanceof IOException) ) {
  @@ -281,8 +293,14 @@
   	    ctx.log( "Handler errorServlet is null! errorPath:" + errorPath);
   	    return;
   	}
  +
   
  -	if (!isDefaultHandler && !res.isBufferCommitted())
  +	// XXX The original code didn't reset the buffer if
  +	// isDefaultHandler :	if (!isDefaultHandler && ...
  +	// Is there any reason for that ?
  +	// I also think we should reset the buffer anyway, to get
  +	// in a stable state - even if the buffer is commited
  +	if ( !res.isBufferCommitted())
   	    res.resetBuffer();
   
   	req.setAttribute("javax.servlet.error.exception_type", t.getClass());
  @@ -294,14 +312,16 @@
   	if( debug>0 )
   	    ctx.log( "Handler " + errorServlet + " " + errorPath);
   
  +	// reset error exception
  +	res.setErrorException( null );
   	errorServlet.service( req, res );
   	Exception ex=res.getErrorException();
  -	if( ! (ex instanceof IOException) ) {
  +	if( ex!=null && ! (ex instanceof IOException) ) {
   	    // we can ignore IOException - probably the user
   	    // has clicked "STOP"
   	    // we need to log any other error - something may be
   	    // broken if the error servlet has errors.
  -	    ctx.log( "Error in errorServlet", ex);
  +	    ctx.log( "Error in errorServlet: ", ex);
   	} 
       }
   
  @@ -379,9 +399,9 @@
   	    req.setNote( sbNote, buf );
   	}
   	
  -	boolean bufReset = (res.getBuffer().getBytesWritten() == 0);
  +	boolean needsHead = res.getBuffer().isNew();
   	// only include <head>...<body> if reset was successful
  -	if (bufReset) {
  +	if (needsHead) {
   	    buf.append("<head><title>")
   		.append(sm.getString("defaulterrorpage.notfound404"))
   		.append("</title></head>\r\n<body>");
  @@ -402,7 +422,7 @@
   	}
   
   	// only add </body> if reset was successful
  -	if ( bufReset )
  +	if ( needsHead )
   	    buf.append("</body>");
   	buf.append("\r\n");
   
  @@ -435,16 +455,16 @@
   	Throwable e= (Throwable)req.
   	    getAttribute("tomcat.servlet.error.throwable");
   	if( e==null ) {
  -	    log("Exception handler called without an exception", new Throwable("trace"));
  +	    log("Exception handler called without an exception",
  +		new Throwable("trace"));
   	    return;
   	}
   
  -	res.setContentType("text/html");
  -	res.setStatus( 500 );
   	
   	if( sbNote==0 ) {
  -	    sbNote=req.getContextManager().getNoteId(ContextManager.REQUEST_NOTE,
  -						     "ExceptionHandler.buff");
  +	    sbNote=req.getContextManager().
  +		getNoteId(ContextManager.REQUEST_NOTE,
  +			  "ExceptionHandler.buff");
   	}
   
   	// we can recycle it because
  @@ -455,9 +475,13 @@
   	    req.setNote( sbNote, buf );
   	}
   
  -	boolean bufReset = (res.getBuffer().getBytesWritten() == 0);
  +	boolean needsHead = res.getBuffer().isNew();
  +
   	// only include <head>...<body> if reset was successful
  -	if (bufReset) {
  +	if ( needsHead ) {
  +	    res.setContentType("text/html");
  +	    res.setStatus( 500 );
  +	
   	    buf.append("<head><title>");
   	    if( null != errorURI && showDebugInfo ) {
   		buf.append(sm.getString("defaulterrorpage.includedservlet") )
  @@ -509,7 +533,7 @@
   	}
   
   	// only add </body> if reset was successful
  -	if ( bufReset )
  +	if (  needsHead )
   	    buf.append("</body>");
   	buf.append("\r\n");
   	
  @@ -557,9 +581,9 @@
   	    req.setNote( sbNote, buf );
   	}
   
  -	boolean bufReset = (res.getBuffer().getBytesWritten() == 0);
  +	boolean needsHead = res.getBuffer().isNew();
   	// only include <head>...<body> if reset was successful
  -	if (bufReset) {
  +	if (needsHead) {
   	    buf.append("<head><title>");
   	    if( null != errorURI && showDebugInfo ) {
   		buf.append(sm.getString("defaulterrorpage.includedservlet") )
  @@ -615,7 +639,7 @@
   	}
   
   	// only add </body> if reset was successful
  -	if ( bufReset )
  +	if ( needsHead )
   	    buf.append("</body>");
   	buf.append("\r\n");