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);
}
}
Aggregations