我正在使用Play!Framework和Java,我想在控制台中显示异常ID和异常的标题,只有那个。
为了测试,我创建了一个像这样的Global对象:
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和标题,但如果我尝试:
throwable.getId(); // does not exists
throwable.getTitle(); // Does not neither
那么,我如何访问id和title?
答案 0 :(得分:2)
对于那个,我看了Play20 code at Github,更准确地说是那两个类:
事实上,Play抛出了一个PlayException,它扩展了UsefullException,包含id,title和description。所以我就是这样做的:
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);
}
}
就是这样! :)