Grails控制器中的异常处理与ExceptionMapper在Grails 2.2.4最佳实践中 [英] Exception handling in Grails controllers with ExceptionMapper in Grails 2.2.4 best practice
问题描述
class ErrorController {
def index(){
def exception = request.exception.cause
def message = ExceptionMapper.mapException(exception)
def status = message.status
response.status = status
render(view:/ error,model:[status:status,exception:exception])
}
}
引发异常:
groovy.lang.MissingPropertyException:没有这样的属性:ExceptionMapper类:ErrorController
grails机制如何控制器异常的一般处理工作?
提出的代码是Grails中的最佳做法/推荐方式?
您从另一个问题复制了一些代码,但是它使用不属于Groovy或Grails的 ExceptionMapper
类(如果您需要导入声明),并没有在答案中定义。我不知道它是做什么的,但是这样的东西应该可以工作:
def exception = request.exception.cause
response.status = 500
render(view:/ error,model:[exception:exception])
While handling exception in Grails 2.2.4 with the scheme reported here:
Exception handling in Grails controllers
class ErrorController {
def index() {
def exception = request.exception.cause
def message = ExceptionMapper.mapException(exception)
def status = message.status
response.status = status
render(view: "/error", model: [status: status, exception: exception])
}
}
an exception is raised:
groovy.lang.MissingPropertyException: No such property: ExceptionMapper for class: ErrorController
How does the grails mechanism for general handling of controller exceptions works?
The code proposed is a best practice/recommended way in Grails?
You copied some code from another question, but it uses an ExceptionMapper
class that isn't part of Groovy or Grails (and if it were you'd need an import statement), and isn't defined in the answer. I'm not sure what it does, but something like this should work:
def exception = request.exception.cause
response.status = 500
render(view: "/error", model: [exception: exception])
这篇关于Grails控制器中的异常处理与ExceptionMapper在Grails 2.2.4最佳实践中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!