Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make some URL mappings depending on the environment?

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 ?

like image 494
fabien7474 Avatar asked May 27 '10 19:05

fabien7474


2 Answers

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')

        }
    }
}
like image 179
tinny Avatar answered Nov 10 '22 06:11

tinny


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.

like image 30
Burt Beckwith Avatar answered Nov 10 '22 07:11

Burt Beckwith