开发者

Is it possible to catch / handle exceptions thrown from a Grails controller? Aop?

class MyController {
   def myAction = {
      throw new MyException("Tes开发者_如何学运维t")
   }
}

Is it possible to catch / handle the exception thrown by the code above? The following url-mapping kinda works, but it causes the exception to be logged, which is annoying because in my case I'm able to handle it.

"500"(controller: "error", action: 'myExceptionHandler', exception: MyException)

Why don't I wrap the code that might throw an exception in try / catch? Well I have several actions that might throw the same exception. Wrapping each and every one of them in try / catch violates the DRY-principle.


How about this awesome pattern.

http://grails.1312388.n4.nabble.com/Possible-to-get-the-errorhandler-calling-a-controller-closure-td1354335.html

class UrlMappings {
    static mappings = {
      "/$controller/$action?/$id?" {
          constraints { // apply constraints here  }
      }
    "500"(controller:'system', action:'error')
}

class SystemController {
    def error = {
        // Grails has already set the response status to 500

        // Did the original controller set a exception handler?
        if (request.exceptionHandler) {
            if (request.exceptionHandler.call(request.exception)) {
                return
            }           
            // Otherwise exceptionHandler did not want to handle it
        }       
        render(view:"/error")        
    }
}

class MyAjaxController {

    def beforeInterceptor = {
        request.exceptionHandler = { ex ->
            //Do stuff           
            return true
        }
    }

    def index = {
        assert false
    }
}


There is a way to have common exception handling using Grails.

First off, in your UrlMapping file you may declare mappings as follows:

"500" (controller: "foo", action: "bar", exception: FooBarException)

Keep in mind however, that this will flood your log files with error messages. Depending on the precise use-case this may or may not be what you want.

If you don't want error messages produced, you can resort to a plugin. It should pop up after putting "declarative exception handling +grails" into google.

Personally, I use a following approach: I route all the abnormal exceptions to ErrorController. There I have access to request["exception"], and some additional parameters like the original controller and action (I put this information into request attribute map manually using a before filter). I than decide to take appropriate steps - usually calling some action on a the original controller, say fatalError.


There may be a more "correct" method of doing this in Grails, but write a method that takes in a closure and handles the exception if needed. Then your code would be something like:

class MyController {
   def myAction = {
      handleMyException {
         throw new MyException("Test")
      }
   }
}

It introduces a line of code, but the exception handling code is at least all in one place, and you have the benefit of handling the exception in the action where it was thrown.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜