Search in sources :

Example 1 with JavacErrorDetail

use of org.apache.sling.scripting.jsp.jasper.compiler.JavacErrorDetail in project sling by apache.

the class JspServletWrapper method handleJspExceptionInternal.

/**
     * Returns only a ServletException or a SlingException
     */
private Exception handleJspExceptionInternal(final Exception ex) throws ServletException {
    Throwable realException = ex;
    String exMessage = "";
    if (ex instanceof ServletException) {
        realException = ((ServletException) ex).getRootCause();
        // root cause might be null (eg. for a JasperException ex)
        if (realException == null) {
            realException = ex;
        } else {
            exMessage = ex.toString();
        }
    }
    // avoid nested ScriptEvaluationExceptions (eg. in nested jsp includes)
    while (realException instanceof ScriptEvaluationException) {
        realException = realException.getCause();
    }
    try {
        // First identify the stack frame in the trace that represents the JSP
        StackTraceElement[] frames = realException.getStackTrace();
        StackTraceElement jspFrame = null;
        for (int i = 0; i < frames.length; ++i) {
            if (frames[i].getClassName().equals(this.theServlet.getClass().getName())) {
                jspFrame = frames[i];
                break;
            }
        }
        if (jspFrame == null) {
            // to the generated servlet class, we can't really add anything
            if (ex instanceof ServletException) {
                return ex;
            }
            return new SlingException(ex) {
            };
        }
        int javaLineNumber = jspFrame.getLineNumber();
        JavacErrorDetail detail = ErrorDispatcher.createJavacError(jspFrame.getMethodName(), this.ctxt.getCompiler().getPageNodes(), null, javaLineNumber, ctxt);
        // If the line number is less than one we couldn't find out
        // where in the JSP things went wrong
        int jspLineNumber = detail.getJspBeginLineNumber();
        if (jspLineNumber < 1) {
            if (realException instanceof ServletException) {
                return (ServletException) realException;
            }
            return new SlingException(exMessage, realException);
        }
        if (options.getDisplaySourceFragment() && detail.getJspExtract() != null) {
            return new SlingException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + jspLineNumber) + "\n\n" + detail.getJspExtract() + "\n", realException);
        }
        return new SlingException(Localizer.getMessage("jsp.exception", detail.getJspFileName(), "" + jspLineNumber), realException);
    } catch (final Exception je) {
        // If anything goes wrong, just revert to the original behaviour
        if (realException instanceof ServletException) {
            return (ServletException) realException;
        }
        return new SlingException(exMessage, realException);
    }
}
Also used : ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) JavacErrorDetail(org.apache.sling.scripting.jsp.jasper.compiler.JavacErrorDetail) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingException(org.apache.sling.api.SlingException) ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) UnavailableException(javax.servlet.UnavailableException) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingPageException(org.apache.sling.scripting.jsp.SlingPageException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException)

Aggregations

IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 SlingException (org.apache.sling.api.SlingException)1 SlingIOException (org.apache.sling.api.SlingIOException)1 SlingServletException (org.apache.sling.api.SlingServletException)1 ScriptEvaluationException (org.apache.sling.api.scripting.ScriptEvaluationException)1 SlingPageException (org.apache.sling.scripting.jsp.SlingPageException)1 JasperException (org.apache.sling.scripting.jsp.jasper.JasperException)1 JavacErrorDetail (org.apache.sling.scripting.jsp.jasper.compiler.JavacErrorDetail)1