Results 1 to 4 of 4

Thread: Mapping to response codes doesn't work

  1. #1

    Default Mapping to response codes doesn't work

    Hi again,

    according to the grails documentation I've got this in my UrlMappings.groovy:

    Code:
    		"403"(controller: "errors", action: "forbidden")
    		"404"(controller: "errors", action: "notFound")
    		"500"(controller: "errors", action: "serverError")
    In my security filter I'm redirecting like this:

    Code:
    redirect(controller: "errors", action: "forbidden")
    However I'm always getting a 404 after the redirect. So I tried adding:

    Code:
    static mappings = {
       "403"(view: "/errors/forbidden")
       "404"(view: "/errors/notFound")
       "500"(view: "/errors/serverError")
    }
    as the redirect points to "/errors/forbidden". However I'm getting a 404 again. In case that ordering matters in some way I added these mappings at the top of the file.

    I ended up with:

    response.sendError(403, "forbidden")

    Which works just fine. But I'm wondering why the other way didn't work. Am I getting something totally wrong or am I just missing a small piece?

    Cheers,
    Thomas

  2. #2
    Join Date
    Jun 2010
    Location
    London
    Posts
    304

    Default

    Quote Originally Posted by ThomasBecker View Post
    Hi again,

    according to the grails documentation I've got this in my UrlMappings.groovy:

    Code:
    		"403"(controller: "errors", action: "forbidden")
    		"404"(controller: "errors", action: "notFound")
    		"500"(controller: "errors", action: "serverError")
    In my security filter I'm redirecting like this:

    Code:
    redirect(controller: "errors", action: "forbidden")
    However I'm always getting a 404 after the redirect.
    Using redirect(), forward(), or chain() like this requires a reverse URL mapping lookup so that Grails knows what the URL for a given controller action is. An error mapping cannot be queried in the reverse direction, since it doesn't correspond to a URL.

    I can see what you are trying to do, but the standard approaches involve either:
    Code:
    response.sendError(404)
    or

    Code:
    render text: "Page not found", status: 404
    Peter

  3. #3

    Default

    Hi Peter,

    ok. I think I'll leave it at response.sendError as it works well.

    Cheers,
    Thomas

  4. #4
    Join Date
    May 2005
    Posts
    23

    Default

    What if I want to display error details in the view? Is it possible to pass a model to the error view using sendError?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •