我知道如何使用UrlMappings在Grails中进行通用异常处理,并使用ErrorController进行泛型异常处理,这样如果异常转义控制器,用户将被发送到通用错误页面并记录异常.我也知道如何使用try/catch块来处理特定异常并尝试从中恢复.
但是在大多数控制器中,如果发生异常,我只想给用户一个稍微更具体的错误消息.所以在创建操作中,我想告诉用户该项目未创建.或者在导入操作中,我想告诉用户导入失败.现在,控制器看起来像:
class ThingController {
def create = {
try {
// The real controller code, which quickly hands it off to a service
} catch (Exception e) {
handleException(e, "There was an error while attempting to create the Thing")
}
}
def delete = {
try {
// The real controller code, which quickly hands it off to a service
} catch (Exception e) {
handleException(e, "There was an error while attempting to delete the Thing")
}
}
private …Run Code Online (Sandbox Code Playgroud)