How to know from where was thrown error 500 (Grails)

David Santamaria picture David Santamaria · Mar 30, 2011 · Viewed 9.5k times · Source

I have the next scenario in my UrlMappings.groovy:

"/user/$action?" (controller:"user")
"/admin/$action?" (controller:"user")

"500"(controller:"error", action:"show")
"404"(controller:"error", action:"show")

And I need to know on the errorController from which controller was thrown the exception (if any) that raises the error 500, and show diferent error pages for users and admin.

Any ideas?

Thanks in advance.

Answer

Aoi Karasu picture Aoi Karasu · Mar 31, 2011

You can access the exception in your ErrorController via request.exception. The top level exception always points to the controller where it was thrown so you can find out the controller name with exception.className. Here's a very simple example.

class ErrorController {

    def show = {
      def exception = request.exception
      render(text: "Exception in ${exception?.className}", 
        contentType: "text/plain", encoding: "UTF-8")
    }
}