由于DRY原因,我在Grails 2.3.11 Controller according to this proposal on SO上使用了动态mixins:
class SomeApiController {
def SomeApiController() {
SomeApiController.mixin MyControllerMixin
}
...
不幸的是,它不适用于异常处理程序,例如:
class MyControllerMixin {
...
def businessException( BusinessException e ) {
log.error( "API exception: ${e.message} ${e.errorCode}", e )
def result = [
status: 'Failure',
errorCode: e.errorCode.name()
]
response.status = 400
render result as JSON
}
...
}
在这种情况下,从某处抛出
SomeApiController
时,将调用BusinessException
中的默认异常处理程序。但是,如果我将处理程序直接放置在 Controller 中,则效果很好,但这显然是我要避免的。是否有任何解决方法也可以使它正常工作?
最佳答案
参见http://grails.org/doc/latest/guide/theWebLayer.html#controllerExceptionHandling。包括以下内容...
Exception handler methods must be present at compile time. Specifically, exception handler methods which are runtime metaprogrammed onto a controller class are not supported.
关于grails - Grails Controller 中使用动态mixins进行异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26450298/