如何跟踪GWT中的JSNI错误?

时间:2014-05-25 18:30:59

标签: java javascript html gwt jsni

当我使用GWT JSNI时,我的JSNI JavaScript代码中可能会出现错误:

try {
...
} catch(error) {
 console.log(error);
}

如何在GWT日志系统或GWT uncaughtException中跟踪此错误?

2 个答案:

答案 0 :(得分:5)

JSNI方法中抛出的异常以JavaScriptExceptions形式暴露给Java代码,如果需要将需要提供给需要的GWT API,您可以轻松地将JS中捕获的对象包装在JavaScriptException中异常(例如GWT.log)。

try {
  …
} catch (e) {
  var ex = @com.google.gwt.core.client.JavaScriptException::new(Ljava/lang/Object;)(e);
  @com.google.gwt.core.client.GWT::log(Ljava/lang/String;Ljava/lang/Throwable;)("Error doing …", ex);
}

在GWT 2.6.1+中,您可以使用JsonLogRecordClientUtilthrowableAsJson(e)将任何异常转换为字符串,例如将其发送到您的服务器或显示它。

在GWT 2.6.0+中,您可以使用GWT.reportUncaughtExceptionUncaughtExceptionHandler报告任何异常(如果有),或者向浏览器报告:

try {
  …
} catch (e) {
  var ex = @com.google.gwt.core.client.JavaScriptException::new(Ljava/lang/Object;)(e);
  @com.google.gwt.core.client.GWT::reportUncaughtException(Ljava/lang/Throwable;)(ex);
}

那就是说,在大多数情况下你不需要这样:只是让异常在你的JSNI方法之外传播并捕获Java-land中的JavaScriptException

答案 1 :(得分:0)

您可以创建一个JSNI方法log()并调用

$wnd.log(JSON.stringify(error));并登录GWT记录器。