Saturday, January 22, 2011

Handling exceptions in Grails

If you ever find yourself in need to provide custom exception handling in your application but you need to do more than just show the view (send an email for example) there's a really simple way to do it. It consists of 2 steps (of which the documentation mentions only one and remains completely silence about the other one):

1. Create a mapping in UrlMappings.groovy:
"500"(controller: 'errors')

2. Create a controller that will handle it:
class ErrorsController {
def index = {
def exception = request.exception
// do some processing here
}
}


It's very unfortunate that the request.exception is not mentioned in the right spot in Grails documentation. It leads to confusion that the actual exception instance is available only in views which is clearly not the case.

1 comment:

Peter Ledbrook said...

Good point. Could you raise a documentation JIRA? Even better would be a patch or GitHub pull request.

Thanks,

Peter