(Quick Reference)

6.4.4 Mapping to Response Codes - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

6.4.4 Mapping to Response Codes

Grails also lets you map HTTP response codes to controllers, actions or views. Just use a method name that matches the response code you are interested in:
Grails tambien le permite mapear codigos de respuesta HTTP hacia los controllers, actions o vistas. Solo use un nombre de metodo que empate el codigo de respuesta en el cual esta interesado:

static mappings = {
   "403"(controller: "errors", action: "forbidden")
   "404"(controller: "errors", action: "notFound")
   "500"(controller: "errors", action: "serverError")
}

Or you can specify custom error pages:
O puede especificar paginas de error personalizadas:

static mappings = {
   "403"(view: "/errors/forbidden")
   "404"(view: "/errors/notFound")
   "500"(view: "/errors/serverError")
}

Declarative Error Handling

Manejo Declarativo de Errores

In addition you can configure handlers for individual exceptions:
Ademas puede configurar los manejadores para excepciones individuales:

static mappings = {
   "403"(view: "/errors/forbidden")
   "404"(view: "/errors/notFound")
   "500"(controller: "errors", action: "illegalArgument",
         exception: IllegalArgumentException)
   "500"(controller: "errors", action: "nullPointer",
         exception: NullPointerException)
   "500"(controller: "errors", action: "customException",
         exception: MyException)
   "500"(view: "/errors/serverError")
}

With this configuration, an IllegalArgumentException will be handled by the illegalArgument action in ErrorsController, a NullPointerException will be handled by the nullPointer action, and a MyException will be handled by the customException action. Other exceptions will be handled by the catch-all rule and use the /errors/serverError view.
Con esta configuracion, una IllegalArgumentException sera manejada por la accion illegalArgument en ErrorsController, una NullPointerException sera manejada por la accion nullPointer, y MyException sera manejada por la accion customException. Otras excepciones seran manejadas por la regla catch-all y usa la vista /errors/serverError.

You can access the exception from your custom error handing view or controller action using the request's exception attribute like so:
Puede acceder a la excepcion desde su vista de error personalizada o la accion del controlador usando el atributo exception de la peticion de esta manera:

class ErrorController {
    def handleError() {
        def exception = request.exception
        // perform desired processing to handle the exception
    }
}

If your error-handling controller action throws an exception as well, you'll end up with a StackOverflowException.
Si su accion del manejador de errores del controller arroja una excepcion tambien, terminara con una StackOverflowException.