Viewing the source of a compiled GSP in Grails

March 15, 2008 – 3:55 pm

So you’re working happily on your Grails application, coding away, being extremely productive until BOOM! You get a Grails Runtime Exception error page like this when you hit a GSP:

grails runtime error

What the heck? Reading the stack trace you see that the exception is caused somewhere in a GSP:

You’ve found the error of course, it’s right there on line 33 of the grails-app/views/person/show.gsp. Um, not quite. If you look at line 33 of show.gsp you won’t find anything of consequence:

<td valign=”top” class=”name”>Name:</td>

There isn’t any code on that line that could possibly cause a null pointer exception. So then you realize - the line 33 the stack trace is referring to is the line in the compiled GSP! But where oh where would that source for the GSP be? You search in your ~/.grails directory but don’t find anything obvious there. A couple fruitless google searches later and you start undoing your changes, one by one, until you find the line of code that caused your problem. What if, you ask, there was an easy way to see the source of a compiled Grails GSP? Then you find out:

there is.

When running in “development” mode (i.e. this trick doesn’t work in test or production or other environments), just append the parameter “showSource” to the URL (e.g. http://localhost:8080/[APP_NAME]/person/show/3?showSource) and TADA - you get the compiled source code:

Now you can figure out where line 33 is (I usually end up copying the source into an editor like TextMate that shows line numbers) and see what caused your error. In this particular case, gender is null and when the GSP tries to obtain the “code” attribute on the “gender” property, it causes a NPE.

Add a quick fix:
person.gender?.code
try the page again and everything’s hunky dory.

Thank you showSource parameter. You just made my life oh so much easier.

  1. 7 Responses to “Viewing the source of a compiled GSP in Grails”

  2. Nice! I didn’t realize you could do this and had briefly looked around previously for the compiled source to my .gsp pages without any luck. This makes it super easy to find what’s going on.

    I bet with a little bit of playing around it’d be possible to modify showSource to give it a line number value so that the resulting source would return just that line (possibly with a few context lines around it).

    By tednaleid on Mar 15, 2008

  3. Thanks for this post. I know this will come in very handy. I’ve worked with some very basic Grails apps used in different tutorials. I did face such long, cryptic error dumps when I started customizing them. Knowing that one can see the source will be be a good time saver.

    But I think better error messages should be on the Grails roadmap… I better head over to the Grails bug tracking system and see if they have something like this listed.

    Cheers again!

    By Indy Nagpal on Mar 16, 2008

  4. wow. that’s an important tip! thanks

    By Tom on Mar 17, 2008

  5. When i tried it, the source of error page was shown. It was a syntax/parse error though..

    By Uysal on Mar 19, 2008

  6. Wow, nice. You just blew my mind. I was wondering how to get at those compiled sources. Is this documented? Wow.

    By Shawn Hartsock on May 13, 2008

  7. As of Grails 1.0.3 line numbers are apparently appended to each line. No more cop/paste to the editor!

    By ClutchControl on Jun 10, 2008

  1. 1 Trackback(s)

  2. Mar 30, 2008: Grails Podcast Episode 51: Newscast for March 29th 2008 « Sven Haiges’ Personal Blog

Post a Comment