开发者

Custom Grails exception handling

开发者 https://www.devze.com 2023-02-07 15:15 出处:网络
Following an example with custom exception handling in Grails, I came to开发者_StackOverflow社区 the following code:

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

精彩评论

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