[英]How to track JSNI Errors in GWT?
When I use GWT JSNI there can be errors in my JSNI JavaScript code like this: 当我使用GWT JSNI时,我的JSNI JavaScript代码可能会出现如下错误:
try {
...
} catch(error) {
console.log(error);
}
How can I track this error in the GWT logging system or with the GWT uncaughtException? 如何在GWT日志记录系统或GWT uncaughtException中跟踪此错误?
Exceptions thrown in JSNI methods are exposed to the Java code as JavaScriptException
s , and you can easily wrap the object caught in JS in a JavaScriptException
if you need to give it to GWT APIs that need an exception (such as GWT.log
). JSNI方法中引发的异常以JavaScriptException
的形式公开给Java代码,如果您需要将它提供给需要异常的GWT API(例如GWT.log
),则可以轻松地将JS中捕获的对象包装在JavaScriptException
。
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);
}
In GWT 2.6.1+ you can use JsonLogRecordClientUtil
. 在GWT 2.6.1+中,您可以使用JsonLogRecordClientUtil
。 throwableAsJson(e)
to turn any exception into a String to, eg send it to your server or display it. throwableAsJson(e)
将任何异常转换为字符串,例如将其发送到您的服务器或显示它。
In GWT 2.6.0+ you can use GWT.reportUncaughtException
to report any exception to the UncaughtExceptionHandler
if any, or to the browser otherwise: 在GWT 2.6.0+中,您可以使用GWT.reportUncaughtException
向UncaughtExceptionHandler
报告任何异常(如果有),或者向浏览器报告任何异常:
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);
}
That said, in most case you don't need this: just let the exception propagate outside your JSNI method and catch the JavaScriptException
in Java-land. 也就是说,在大多数情况下,您不需要这样做:只需让异常传播到JSNI方法外部,并在Java领域捕获JavaScriptException
。
You can create an JSNI method log()
and call 您可以创建一个JSNI方法log()
并调用
$wnd.log(JSON.stringify(error));
and log to the GWT logger. 并登录到GWT记录器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.