larryi 00/12/20 07:20:22 Modified: src/share/org/apache/tomcat/context ErrorHandler.java Log: Update default error handlers to use showDebugInfo property to control what is displayed. Also, if buffer was reset or no output generated, the error output will include head, title, and body tags. Revision Changes Path 1.13 +115 -24 jakarta-tomcat/src/share/org/apache/tomcat/context/ErrorHandler.java Index: ErrorHandler.java =================================================================== RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/context/ErrorHandler.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- ErrorHandler.java 2000/12/13 19:58:00 1.12 +++ ErrorHandler.java 2000/12/20 15:20:21 1.13 @@ -170,7 +170,7 @@ return; } - if (!isDefaultHandler) + if (!isDefaultHandler && !res.isBufferCommitted()) res.resetBuffer(); req.setAttribute("javax.servlet.error.status_code",new Integer( code)); @@ -267,11 +267,12 @@ return; } - if (!isDefaultHandler) + if (!isDefaultHandler && !res.isBufferCommitted()) res.resetBuffer(); req.setAttribute("javax.servlet.error.exception_type", t.getClass()); req.setAttribute("javax.servlet.error.message", t.getMessage()); + req.setAttribute("javax.servlet.jsp.jspException", t); req.setAttribute("tomcat.servlet.error.throwable", t); req.setAttribute("tomcat.servlet.error.request", req); @@ -361,15 +362,32 @@ req.setNote( sbNote, buf ); } - buf.append("<head><title>") - .append(sm.getString("defaulterrorpage.notfound404")) - .append("</title></head>\r\n"); - buf.append("<body><h1>") + boolean bufReset = (res.getBuffer().getBytesWritten() == 0); + // only include <head>...<body> if reset was successful + if (bufReset) { + buf.append("<head><title>") + .append(sm.getString("defaulterrorpage.notfound404")) + .append("</title></head>\r\n<body>"); + } + buf.append("<h1>") .append(sm.getString("defaulterrorpage.notfound404")) .append("</h1>\r\n"); buf.append(sm.getString("defaulterrorpage.originalrequest")) - .append( requestURI ); - buf.append("</body>\r\n"); + .append( requestURI ) + .append("\r\n"); + + if ( null != requestURI && contextM.isShowDebugInfo() ) { + buf.append("<br><br>\r\n<b>") + .append(sm.getString("defaulterrorpage.notfoundrequest")) + .append("</b> ") + .append( requestURI ) + .append("\r\n"); + } + + // only add </body> if reset was successful + if ( bufReset ) + buf.append("</body>"); + buf.append("\r\n"); res.setContentLength(buf.length()); @@ -393,6 +411,7 @@ throws Exception { String msg=(String)req.getAttribute("javax.servlet.error.message"); + String errorURI = res.getErrorURI(); Throwable e= (Throwable)req. getAttribute("tomcat.servlet.error.throwable"); @@ -416,8 +435,22 @@ buf = new StringBuffer(); req.setNote( sbNote, buf ); } + + boolean bufReset = (res.getBuffer().getBytesWritten() == 0); + // only include <head>...<body> if reset was successful + if (bufReset) { + buf.append("<head><title>"); + if( null != errorURI && contextM.isShowDebugInfo() ) { + buf.append(sm.getString("defaulterrorpage.includedservlet") ) + .append(" "); + } else { + buf.append("Error: "); + } + buf.append( 500 ) + .append("</title></head>\r\n<body>\r\n"); + } buf.append("<h1>"); - if( res.isIncluded() ) { + if( null != errorURI && contextM.isShowDebugInfo() ) { buf.append(sm.getString("defaulterrorpage.includedservlet") ). append(" "); } else { @@ -433,18 +466,32 @@ .append(req.requestURI().toString()) .append("</h2>"); - buf.append("<b>") - .append(sm.getString("defaulterrorpage.internalservleterror")) - .append("</b><br>"); - - buf.append("<pre>"); - // prints nested exceptions too, including SQLExceptions, recursively - String trace = Logger.throwableToString - (e, "<b>" + sm.getString("defaulterrorpage.rootcause") + "</b>"); - buf.append(trace); + if ( null != errorURI && contextM.isShowDebugInfo()) { + buf.append("\r\n<h2>") + .append(sm.getString("defaulterrorpage.errorlocation")) + .append(" ") + .append(errorURI) + .append("</h2>"); + } + + if (contextM.isShowDebugInfo()) { + buf.append("<b>") + .append(sm.getString("defaulterrorpage.internalservleterror")); + buf.append("</b><br>\r\n<pre>"); + // prints nested exceptions too, including SQLExceptions, recursively + String trace = Logger.throwableToString + (e, "<b>" + sm.getString("defaulterrorpage.rootcause") + "</b>"); + buf.append(trace); + buf.append("</pre>\r\n"); + } else { + buf.append("<b>Error:</b> ") + .append(e.getMessage()) + .append("<br><br>\r\n"); + } - buf.append("</pre>\r\n"); - + // only add </body> if reset was successful + if ( bufReset ) + buf.append("</body>"); buf.append("\r\n"); res.getBuffer().write( buf ); @@ -469,6 +516,7 @@ throws Exception { String msg=(String)req.getAttribute("javax.servlet.error.message"); + String errorURI = res.getErrorURI(); res.setContentType("text/html"); // res is reset !!! @@ -487,9 +535,24 @@ buf = new StringBuffer(); req.setNote( sbNote, buf ); } + + boolean bufReset = (res.getBuffer().getBytesWritten() == 0); + // only include <head>...<body> if reset was successful + if (bufReset) { + buf.append("<head><title>"); + if( null != errorURI && contextM.isShowDebugInfo() ) { + buf.append(sm.getString("defaulterrorpage.includedservlet") ) + .append(" "); + } else { + buf.append("Error: "); + } + buf.append( sc ) + .append("</title></head>\r\n<body>\r\n"); + } buf.append("<h1>"); - if( res.isIncluded() ) { - buf.append(sm.getString("defaulterrorpage.includedservlet") ); + if( null != errorURI && contextM.isShowDebugInfo() ) { + buf.append(sm.getString("defaulterrorpage.includedservlet") ) + .append(" "); } else { buf.append("Error: "); } @@ -502,10 +565,38 @@ .append(sm.getString("defaulterrorpage.location")) .append(req.requestURI().toString()) .append("</h2>"); + + if ( sc >= 400 && errorURI != null && contextM.isShowDebugInfo()) { + buf.append("\r\n<h2>") + .append(sm.getString("defaulterrorpage.errorlocation")) + .append(" ") + .append(errorURI) + .append("</h2>"); + } - buf.append("<b>") + buf.append("\r\n<b>") .append(msg) - .append("</b><br>"); + .append("</b><br>\r\n"); + + // add unavailable time if present + if ( sc == 503) { + Integer ut = (Integer)req.getAttribute("tomcat.servlet.error.service.unavailableTime"); + if ( ut != null) { + buf.append("<br>"); + // if permanent + if (ut.intValue() < 0) { + buf.append(sm.getString("defaulterrorpage.service.permanently.unavailable")); + } else { + buf.append(sm.getString("defaulterrorpage.service.unavailable",ut)); + } + buf.append("<br>\r\n"); + } + } + + // only add </body> if reset was successful + if ( bufReset ) + buf.append("</body>"); + buf.append("\r\n"); res.setContentLength(buf.length()); res.getBuffer().write( buf );