Search code examples
grailsurl-mapping

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 ?


Solution

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