views:

93

answers:

2

When getting an HTTP status code 500, I want to display 2 different pages according to the running environment.

In development mode, I want to display a stackStrace page (like the default Grails 500 error page) and in production mode, I want to display a formal "internal error" page.

Is it possible and how can I do that ?

+2  A: 

There may be a cleaner way to do this, but I'd got with mapping the error code to a controller and handling the logic there:

class UrlMappings {

   static mappings = {

      "/$controller/$action?/$id?" { constraints {} }

      "/"(view:"/index")

      "403"(controller: "errors", action: "accessDenied")
      "404"(controller: "errors", action: "notFound")
      "405"(controller: "errors", action: "notAllowed")
      "500"(view: '/error')
   }
}

and then create the corresponding controller (grails-app/conf/controllers/ErrorsController.groovy):

import grails.util.Environment

class ErrorsController extends AbstractController {

   def accessDenied = {}

   def notFound = {}

   def notAllowed = {}

   def serverError = {
      if (Environment.current == Environment.DEVELOPMENT) {
         render view: '/error'
      }
      else {
         render view: '/errorProd'
      }
   }
}

You'll need to create the corresponding GSPs in grails-app/views/errors (accessDenied.gsp, notFound.gsp, etc.) and also the new grails-app/views/errorProd.gsp. By routing to a controller method for all error codes you make it easier to add logic to the other error code handlers in the future.

Burt Beckwith
Thank you very much Burt.
fabien7474
A: 

You can do environment specific mappings within UrlMappings.groovy

grails.util.GrailsUtil to the rescue

Its not pretty, but I think it will solve your issue

E.g

import grails.util.GrailsUtil

class UrlMappings {
    static mappings = {


        if(GrailsUtil.getEnvironment() == "development") {

             "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/devIndex")
            "500"(view:'/error')
        }

        if(GrailsUtil.getEnvironment() == "test") {
            "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/testIndex")
            "500"(view:'/error')

        }



        if(GrailsUtil.getEnvironment() == "production") {
            "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/prodIndex")
            "500"(view:'/error')

        }
    }
}
tinny
Thx. I'll try it and update this thread. Is it not possible to have the if condition ONLY for "500" mapping?
fabien7474
Yes. The above is just an example
tinny
thank you very much. It works!
fabien7474