hfhc2
hfhc2

Reputation: 4391

Grails error handling in controllers

I would like to render custom JSON error messages. I created the following controller:

class ErrorController {

    static responseFormats = ['json']

    def notAuthorized() {
        render([error: "You are not authorized to access ${request.getRequestURL()} on this server.",
                path: request.getRequestURI()] as JSON)
    }
}

Inside the UrlMappings class I have the following:

"401"(controller: 'error', action: 'notAuthorized')

Everything works alright, except for the fact that I GET http://localhost/path the following error message:

{"error":"You are not authorized to access http://localhost:8080/error on this server.",
 "path":"/error"}

So the URI/URL in the request object does not point to the URI/URL given by the request. Is there a way to obtain the correct URI/URL?

Upvotes: 0

Views: 168

Answers (1)

Gregg
Gregg

Reputation: 35864

I think what you want is request.forwardURI. That should give you the request that triggered the error.

Upvotes: 2

Related Questions