gpt4 book ai didi

java - 如何跟踪 GWT 中的 JSNI 错误?

转载 作者:行者123 更新时间:2023-11-29 07:47:57 26 4
gpt4 key购买 nike

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

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

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

最佳答案

JSNI 方法中抛出的异常以 JavaScriptExceptions 的形式暴露给 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);
}

在 GWT 2.6.1+ 中你可以使用 JsonLogRecordClientUtil .throwableAsJson(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 领域捕获 JavaScriptException

关于java - 如何跟踪 GWT 中的 JSNI 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23858601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com