Skip to content

Instantly share code, notes, and snippets.

@olivermt
Created July 19, 2011 16:15
Show Gist options
  • Select an option

  • Save olivermt/1092930 to your computer and use it in GitHub Desktop.

Select an option

Save olivermt/1092930 to your computer and use it in GitHub Desktop.
Exception conundrum
/*
Problem:
I want to leak exceptions that should not happen, so that the "500" mapping simply shows an error page.
In my 500 page action, i check for ?showException=1, and if its there, I render the stock error.gsp.
Now all this is fine and dandy, except the fact that I want to embed the perpetrator into the exception
so I can list the errors.
Code so far:
*/
//exception class
class UserException extends RuntimeException{
def embeddedObject
}
//The important parts of my changed errors.gsp
<g:if test="${exception}">
<g:if test="${exception?.embeddedObject}"> //where it fails
<h2>Errors</h2>
<g:renderErrors bean="${exception.embeddedObject?.errors}"></g:renderErrors>
//more stuff downwards here, but it fails anyways
//My test action, just throwing the exception to see,
def create = {
def cmd = new ChangePasswordCommand(newPassword: "123", newPasswordConfirm:"asdas", forgottenPasswordHash:"123o812")
cmd.validate()
throw new UserException(embeddedObject: cmd)
}
//the page view that shows the exception
def error = {
if(params.showException)
{
render (view:"/error")
return
}
//other stuff here
}
/*
And so, the error i end up with:
Webpage simply shows: "Internal server error"
Console in STS shows:
2011-07-19 18:13:21,770 [http-8080-2] ERROR view.GroovyPageView - Error processing GroovyPageView: No such property: embeddedObject for class: org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException
Now.. what I dont get, is why this happens..
Grailswrapped... is just an extension of GrailsException, which again is just an extension of RuntimeException.
I'm guessing that somwhere in the conversion from RuntimeException to the uncaught GrailsException, my embeddedObject property disappears.
How do I fix this? Help!
*/
@olivermt
Copy link
Author

Note to self, problem solved by calling getCause on the wrapped grails exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment