8

I'm having some problems getting Grails to map my 404 errors to an errors controller like in the documentation. I'm running Grails 1.3.5 and when I add the following mapping to a default application:

"404" (controller:'errors', action:'notFound')

It works for mapping 500 errors but not 404's. I seem to recall having this problem before and it being related to Tomcat (vs Jetty) but I don't remember a fix or I thought it might have been resolved by now.

I try accessing a resource that's not defined like http://localhost:8080/appName/controllerName/blah and all I get is the default Tomcat 404.

I'm doing a standard grails run-app for testing and trying to get it to work.

ahanson
  • 2,108
  • 3
  • 23
  • 38
  • I've now tried to reproduce the issue, practically, however, everything I tried (forward to a view, forward to controller/action; run-app, production mode WAR) worked as expected. Can you provide a reproducible test? – robbbert Nov 19 '10 at 19:11
  • The issue isn't reproducible for me (tried a lot). - The questioner didn't provide feedback at all. - Voting the question down. - Thanks. – robbbert Dec 02 '10 at 03:47
  • Breakthough! It seems to work for me if I redirect in the error controller method but not if I try to render a page. I will try and setup a bitbucket project and upload an example. – ahanson Dec 02 '10 at 16:49
  • I'm getting this in Grails 1.3.6... – Josh K Jan 29 '11 at 06:48

3 Answers3

8

after deleting whitespace, the problem solved

"404"(controller:'errors', action:'notFound')

Kerem
  • 995
  • 7
  • 19
0

http://jira.codehaus.org/browse/GRAILS-4232 I think this is a known issue

Lifeweaver
  • 986
  • 8
  • 29
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • I'd just converted the attached test case from Grails 1.1 to 1.3.5, and ran the WAR in Tomcat. - The issue doesn't appear to be reproducible anymore. – robbbert Nov 19 '10 at 19:05
0

Add the following code to your application's scripts/Events.groovy:

import groovy.xml.StreamingMarkupBuilder

//modify the generated web.xml so that it supports being mapped to 'error'
eventWebXmlEnd = {String tmpfile ->
    //find the filter mapping to change
    String filterNm = "grailsWebRequest"
    def root = new XmlSlurper().parse(webXmlFile)
    def gwr = root."filter-mapping".find { it."filter-name" == filterNm }
    if (!gwr.size()) throw new RuntimeException(
        "[fail] No Filter named $filterNm")

    // xml is as expected, now modify it and write it back out
    gwr.appendNode {
        dispatcher("ERROR")
    }
    // webXmlFile is an implicit variable created before event is invoked
    webXmlFile.text = new StreamingMarkupBuilder().bind {
        mkp.declareNamespace("": "http://java.sun.com/xml/ns/j2ee")
        mkp.yield(root)
    }
}

See this post for an explanation. Note that I've copied the above script from that posting, but it had to be amended as the web.xml's structure appears to have changed since the time of the posting's writing.

robbbert
  • 2,183
  • 15
  • 15
  • Sorry, I was wrong. - I did some actual testing now and found that this is not necessary with Grails 1.3.5. - Using a customized 404 mapping worked for me when forwarding to either a *view* or a *controller* and *action*. - Practically, I cannot reproduce any problems with 404 URL mappings in Grails. – robbbert Nov 19 '10 at 19:04