Search in sources :

Example 1 with SmapInput

use of org.apache.jasper.compiler.SmapInput in project tomcat by apache.

the class JspServletWrapper method handleJspException.

/**
 * <p>Attempts to construct a JasperException that contains helpful information
 * about what went wrong. Uses the JSP compiler system to translate the line
 * number in the generated servlet that originated the exception to a line
 * number in the JSP.  Then constructs an exception containing that
 * information, and a snippet of the JSP to help debugging.
 * Please see https://bz.apache.org/bugzilla/show_bug.cgi?id=37062 and
 * http://www.tfenne.com/jasper/ for more details.
 *</p>
 *
 * @param ex the exception that was the cause of the problem.
 * @return a JasperException with more detailed information
 */
protected JasperException handleJspException(Exception ex) {
    try {
        Throwable realException = ex;
        if (ex instanceof ServletException) {
            realException = ((ServletException) ex).getRootCause();
        }
        // Find the first stack frame that represents code generated by
        // Jasper
        StackTraceElement[] frames = realException.getStackTrace();
        StackTraceElement jspFrame = null;
        String servletPackageName = ctxt.getBasePackageName();
        for (StackTraceElement frame : frames) {
            if (frame.getClassName().startsWith(servletPackageName)) {
                jspFrame = frame;
                break;
            }
        }
        SmapStratum smap = null;
        if (jspFrame != null) {
            smap = ctxt.getCompiler().getSmap(jspFrame.getClassName());
        }
        if (smap == null) {
            // smap to hand, we can't really add anything
            return new JasperException(ex);
        }
        @SuppressWarnings("null") int javaLineNumber = jspFrame.getLineNumber();
        SmapInput source = smap.getInputLineNumber(javaLineNumber);
        // where in the JSP things went wrong
        if (source.getLineNumber() < 1) {
            throw new JasperException(ex);
        }
        JavacErrorDetail detail = new JavacErrorDetail(jspFrame.getMethodName(), javaLineNumber, source.getFileName(), source.getLineNumber(), null, ctxt);
        if (options.getDisplaySourceFragment()) {
            return new JasperException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + source.getLineNumber()) + System.lineSeparator() + System.lineSeparator() + detail.getJspExtract() + System.lineSeparator() + System.lineSeparator() + "Stacktrace:", ex);
        }
        return new JasperException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + source.getLineNumber()), ex);
    } catch (Exception je) {
        // If anything goes wrong, just revert to the original behaviour
        if (ex instanceof JasperException) {
            return (JasperException) ex;
        }
        return new JasperException(ex);
    }
}
Also used : ServletException(jakarta.servlet.ServletException) SmapInput(org.apache.jasper.compiler.SmapInput) JavacErrorDetail(org.apache.jasper.compiler.JavacErrorDetail) JasperException(org.apache.jasper.JasperException) SmapStratum(org.apache.jasper.compiler.SmapStratum) UnavailableException(jakarta.servlet.UnavailableException) ServletException(jakarta.servlet.ServletException) JasperException(org.apache.jasper.JasperException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ServletException (jakarta.servlet.ServletException)1 UnavailableException (jakarta.servlet.UnavailableException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 JasperException (org.apache.jasper.JasperException)1 JavacErrorDetail (org.apache.jasper.compiler.JavacErrorDetail)1 SmapInput (org.apache.jasper.compiler.SmapInput)1 SmapStratum (org.apache.jasper.compiler.SmapStratum)1