gpt4 book ai didi

grails - Grails Controller 中使用动态mixins进行异常处理

转载 作者:行者123 更新时间:2023-12-02 14:42:38 25 4
gpt4 key购买 nike

由于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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com