Question

I've tried to create a custom 404 url mapping for URL's that are not found:

"/test" {
    controller="test"
}
"404" {
    controller="application"
    action="send404"
}
"500" {
    controller="application"
    action="send500"
}

But for some reason, the controller and action are never called. I get the default container 404 page. So, instead I tried:

"/test" {
    controller="test"
}
"/**" {
    controller="application"
    action="send404"
}
"500" {
    controller="application"
    action="send500"
}

Which seems to work fine, except that it also seems to call the send404 action on every request. For example, if I hit /test, I see the test page, but I also get the log statement I made in the send404() action.

Ideas appreciated...

Was it helpful?

Solution 3

Perhaps it's favicon.ico that's being requested by the browser on each request that's causing this to happen.

// Route 404 to this action to see!
def send404() {

    log.error("404 Page Not Found! " + request.forwardURI)
    response.status = 404;
    render(view:"/application/not-found.gsp")        
}

OTHER TIPS

Have you tried killing whitespace in your declaration, as outlined in this answer?

"404"(controller:'application', action:'send404')

There is also an open issue GRAILS-4232 about this topic.

In grails, there is an ErrorController, the one that render stacktrace on 500, etc.

class UrlMappings {
  static mappings {
    "403" (controller: "error", action: "forbidden")
    "404" (controller: "error", action: "notFound")
    "500" (controller: "error", action: "internalError")
  }
}

And then, you can render(controller:"error", action"notFound") in another controller to stay RESTful. Or it will automagically render the notFound action of the error controller.

More details here : http://groovy.dzone.com/articles/grails-exception-handling-http

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top