开发者

Custom Grails exception handling

Following an example with custom exception handling in Grails, I came to开发者_StackOverflow社区 the following code:

exceptionHandler.exceptionMappings = [
    'my.project.AccessDeniedException': '/accessDenied',
    'my.project.NoSessionException' : '/accessDenied',
    'java.lang.Exception': '/errorProduction'
]

This works fine for the first two types of exceptions, but all other exceptions, like GroovyPagesException, are not handled by Grails anymore, they are handled by the servlet container.

How can I handle all exceptions with Grails (1.3)?


This works:

UrlMappings.groovy:

"500"(controller: 'errors', action: 'handle')

And the controller:

class ErrorsController {

    def handle = {
        def exception = request.exception.cause.class
        if(exception == my.project.NoSessionException ||
               exception == my.project.AccessDeniedException)
            render(view: '/accessDenied')
        else
            render(view: '/errorProduction')
    }

}


Wouldn't you want to surface a 500 error page for such errors? I've used the UrlMappings configuration to override certain kinds of errors with custom pages.

  "500"(controller: "error", action: "cartGone", exception: CartGoneException)    

Curious what the requirement is here?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜