Dav*_*ria 6 error-handling grails url-mapping
我在UrlMappings.groovy中有下一个场景:
"/user/$action?" (controller:"user")
"/admin/$action?" (controller:"user")
"500"(controller:"error", action:"show")
"404"(controller:"error", action:"show")
Run Code Online (Sandbox Code Playgroud)
我需要知道errorController从哪个控制器抛出引发错误500的异常(如果有的话),并为用户和管理员显示不同的错误页面.
有任何想法吗?
提前致谢.
Aoi*_*asu 12
您可以通过ErrorController访问异常request.exception.顶级异常始终指向抛出它的控制器,以便您可以找到控制器名称exception.className.这是一个非常简单的例子.
class ErrorController {
def show = {
def exception = request.exception
render(text: "Exception in ${exception?.className}",
contentType: "text/plain", encoding: "UTF-8")
}
}
Run Code Online (Sandbox Code Playgroud)
使用request.getAttribute("exception")你将有例外.我将查看所有请求属性,也许直接引用原始控制器.
UPDATE
诀窍是Grails将抛出的异常包装到GrailsWrappedRuntimeException中,从而提供对负责异常的代码的舒适访问.在错误控制器中使用以下代码段:
import org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException
def action = {
def exception = request.getAttribute('exception')
if (exception instanceof GrailsWrappedRuntimeException) {
log.error "exception $exception.className, line $exception.lineNumber has throw $exception.cause"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8933 次 |
| 最近记录: |