我正在使用Java的Play!框架,我想在控制台中显示异常ID和异常的标题,仅此而已。
为了测试,我创建了一个全局对象,如下所示:
public class Global extends GlobalSettings {
@Override
public Result onError(RequestHeader request, Throwable throwable) {
Logger.info(Json.toJson(throwable).toString());
return super.onError(request, throwable);
}
}
这将输出一个JSON格式值Throwable,其中包含以下内容:
{
"cause": { ... },
"stackTrace": [ ... ],
"title": "Execution exception",
"description": "[NullPointerException: null]",
"id": "6epj67c35",
"message": "Execution exception[[NullPointerException: null]]",
"localizedMessage": "Execution exception[[NullPointerException: null]]",
"suppressed": []
}
这证明id和title是可访问的,但如果我尝试:
throwable.getId(); // does not exists
throwable.getTitle(); // Does not neither
那么,我该如何访问id和title呢?
发布于 2013-07-09 19:40:54
对于这一点,我查看了Play20 code at Github,更准确地说是这两个类:
事实上,Play抛出了一个扩展UsefullException的PlayException,其中包含id、标题和描述。下面是我是如何做到的:
public class Global extends GlobalSettings {
@Override
public Result onError(RequestHeader request, Throwable throwable) {
if (throwable instanceof PlayException) {
Logger.info(((PlayException) throwable).id);
}
return super.onError(request, throwable);
}
}
就是这样!:)
https://stackoverflow.com/questions/17544191
复制相似问题