Adding the stack trace to a Java Server Page error page
I’ve recently been switched to a Java project and one of the most frustrating parts of the application (other than barely knowing Java) is the error page would only show a “An error as occurred” message and not the actual stack trace. In production this is a perfectly valid scenario, but when developing having to go back to RAD and scroll through the console to find the error message was wasting a lot of time, so after a decent amount of googling I found a way to dump the stack trace to the page.
The code ended up looking like this. In the message board post they used exception instead of error, but hopefully you get the point.
<jsp:useBean id="error" scope="request" class="java.lang.Throwable" />
<%
Object billingError = request.getSession().getAttribute(RequestParamConstants.UNKNOWN_BILLING_ERROR);
error = (Throwable)billingError;
%>
<%@page isErrorPage="true" import="java.io.*"%>
<pre>
<%
error.printStackTrace(new PrintWriter(out));
%>
</pre>
A while after implementing this I ran into an error where the first line was about 400 characters long, so I had to scroll way over to the right. This is because by default the pre tag does not wrap, so I added this simple css fix which allows the pre tag to wrap
pre {
white-space: pre-wrap;
}