0

I'm playing around with error pages per status code and am having troubles rendering views.

I'm using the normal URLMappings strategy for this and using an errors controller to do additional processing and render the view (I'm starting with 500 but will add more obviously):

"500"(controller: 'error', action: 'internalServerError')

In my controller I'm just rendering the views associated with the error:

def internalServerError() {
    render view: '/error/internalServerError'
}

The view isn't anything special, just contains a simple message and such. When I plant something in my code that causes an exception, my controller action above does get called but for some reason render doesn't cause the view to render. It basically just eats the exception and from the user perspective it seems like nothing happened.

I've been tinkering with different rendering options and returns but the same thing always happens. Also worth noting that changing URLMappings to:

"500"(view:'/error/internalServerError')

causes the same thing to happen. Am I missing something here?

2 Answers 2

4

Render the view directly instead of providing the relative path in the action:-

def internalServerError() {
    render view: 'internalServerError'
}

with grails-app/views/error/internalServerError.gsp in place.

UPDATE
Here is how it works:

//UrlMapping:
"500"(controller: 'errors', action: 'internalServerError')

//ErrorsController:
class ErrorsController {
    def index() { 
        response.sendError(500)
    }

    def internalServerError(){
        render view: 'internalServerError'
    }
}

//grails-app/views/errors/internalServerError.gsp
This is my customized Internal Server Error.

Try hitting the index action of the controller.
http://localhost:8080/yourApp/errors

You should see your customized error page.

The same holds good if you are trying to handle Exceptions, like
"500"(controller: 'errors', action: 'internalServerError', exception: Exception)

def index(){
    throw new Exception()
}

will yield the same result.

Note:-
You would need to override or remove this default grails provided entry from UrlMapping.groovy
//"500"(view:'/error')

Sign up to request clarification or add additional context in comments.

5 Comments

In that case, can you post your actual controller and gsp? As you have already mentioned that you are able to hit the action on Exception (making sure that a 500 HTTP response code is present), I should not bother about the wiring.
Your updated code is essentially the same as what I am trying. I hadn't tried the exception parameter in the UrlMappings but that also yields the same result. In all cases the controller action that the "500" mapping points to is always hit, just for some reason render doesn't seem to work.. Thanks for the replies though.
@JacobASeverson It worked for me swiftly rendering the desired page (Grails 2.2.2). Have you removed the default mapping, as mentioned in my Note?
I did. Another thing to note that I discovered today..When the exception happens in a controller or service I see this odd behavior. If the problem happens in a gsp (if I reference an undefined variable or do something else that throws the exception) it actually renders my error page as expected.
Partially resolved, see answer I just added. +1 for URLMappings discussion. Unfortunately it's just a bug in the app and the code mappings were always correct.
1

Looks like it is something unrelated to Grails rendering or URL mappings. It is happening in some parts of the app and not others so there is a bug. Keeping alive for the URLMappings discussion.

Comments