views:

345

answers:

1

In my rails app that I am porting to grails whenever an unexpected error occurs I intercept the error automatically and display a form to the user informing them that an error has occured and asking them for further information. Meanwhile, as the form is rendered I write the stack trace and other information about who was logged in to a database table. Then if the form is submitted I add that information to the error report.

I cannot tell from the exceptionHandler documentation and BootStrap examples whether that will allow me to grab all the information including various session and request parameters and then stuff them into a database and then post a form.

Any thoughts?

A: 

You can use a controller to handle exceptions instead of directly going to error.gsp by changing the '500' code mapping in grails-app/conf/UrlMappings.groovy from

"500"(view:'/error')

to

"500"(controller: 'errors', action: 'error')

Run 'grails create-controller errors' and add the 'error' action:

class ErrorsController {

   def error = {
      def exception = request['javax.servlet.error.exception']?.cause?.cause
      if (exception) {
         // handle exception
      }
   }
}

Since you're now in a controller you can access the request, etc. and do whatever database or other post-handling work you like.

Burt Beckwith
Hello Burt,It goes to the controller I want, but there are no params in the request that allow me to find the original error. Does this work in grails 1.1.1?
Andrew
Sorry, that was for 1.2. In 1.2 there's an extra wrapper around the execptions - in 1.1 there's just one wrapper. So remove the 2nd "?.cause" and it'll work in 1.1.
Burt Beckwith