3

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...

sparkyspider
  • 13,195
  • 10
  • 89
  • 133
  • I also notice that if I add a println() statment to both the TestController.index() and the ApplicationController.send500() actions, and then hit /test, both actions print to the console for the same request. Very strange. return() statement in the action didn't help... – sparkyspider Nov 23 '12 at 10:26
  • Just make sure you looked on all the possiblities from http://grails.1312388.n4.nabble.com/URL-Mapping-error-codes-td1354127.html – Sajan Chandran Dec 18 '12 at 17:18
  • braces instead of brackets? as far as i have seen and used only brackets, never a closure. have you tried with brackets? – aldrin Dec 18 '12 at 18:08

3 Answers3

2

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.

Community
  • 1
  • 1
david
  • 2,529
  • 1
  • 34
  • 50
  • Yup, no such luck. Thanks for the answer though. – sparkyspider Nov 23 '12 at 10:23
  • I have it defined this way "/"{ controller="login" action ="index" } "500"(view:'/error') and it works for me. After you define it this way, please make sure to do a grails clean. Also can you add these statements to the end. – allthenutsandbolts Dec 19 '12 at 14:36
  • In fact, that was the problem why "404" wasn't working, but that didn't stop /** from working. Turns out that there was a bloody favicon.ico being requested each time. See my post below. – sparkyspider Dec 20 '12 at 17:46
1

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

moskiteau
  • 1,104
  • 11
  • 19
  • is there an actual ErrorController.groovy? From what I understand, we have to manually create an ErrorController. – aldrin Dec 19 '12 at 06:40
0

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")        
}
sparkyspider
  • 13,195
  • 10
  • 89
  • 133