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

Using an if statement to conditionally run a target in Hudson

We’re running a horribly outdated version of Hudson at work (2.2.1), so this may not be relevant for people using a version of Hudson updated in the past 4 years. I was trying to conditionally run a block of code in a Hudson build and thought by setting parameters in the Ant build properties I could turn off running unit tests (based on looking at other build XML files).

I had this code in my Ant build properties

do.test=false

This didn’t work, so I echoed the value into the console and it was correctly registering at false, but the tests were still being ran.

 <echo message="Do test? ${do.test}" />
  <target name="test" depends="compile" description="execute unit and functional tests" if="do.test">
    <cobertura-instrument todir="${temp.dir}/instrumented-classes" datafile="${temp.dir}/cobertura.ser">
    </cobertura-instrument>
  </target>

After quite a while trying to find an answer through Google I found out if there’s a parameter called do.test with ANY value then that is a true statement and the block will run. My solution was to rename the parameter to _do.test. You could obviously also delete the parameter, but once I had resolved my build issue I was going to turn the tests back on.