java - GraalVM 中的异常链
问题描述
我正在使用 GraalVM 执行 JavaScript 文件,但我在处理异常时遇到了问题。我的 JS 代码调用回 Java,如果从这些 Java 方法之一抛出异常,那么我会丢失原因链。
public class Example {
public static void doSomething() {
throw new RuntimeException("Example", new RuntimeException("Some nested exception"));
}
}
// --------------
var Example = Java.type("ex.Example");
function f() {
Example.doSomething();
}
// -------------
String src = ...
Source s = Source.newBuilder("js", src, "example").build();
try {
context.eval(s);
} catch (PolyglotException e) {
e.printStackTrace(); // This only prints the PolyglotException with the message "Example"
}
发生这种情况的原因是因为 Graal/Truffle 创建了 的实例HostException
,该实例有一个不调用的构造函数super(e)
,它将其分配给用于获取消息的内部字段,仅此而已。这似乎是故意的,但我不明白原因。这是安全问题吗?你能想出一种方法来改变这种行为吗?我非常希望在我的日志中提供异常的全部原因,但目前它停在HostException
,通常只是说类似“ ”(例如,如果错误的A
原始原因是NoSuchElementException("A")
final class HostException extends RuntimeException implements TruffleException {
private final Throwable original;
HostException(Throwable original) {
this.original = original;
}
Throwable getOriginal() {
return original;
}
@Override
public String getMessage() {
return getOriginal().getMessage();
}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
public Node getLocation() {
return null;
}
public boolean isCancelled() {
return getOriginal() instanceof InterruptedException;
}
}
解决方案
我也遇到过同样的问题,结果发现 JS Error 具有以下功能:
printStackTrace: [Function],
fillInStackTrace: [Function],
getCause: [Function],
initCause: [Function],
toString: [Function],
getMessage: [Function],
getLocalizedMessage: [Function],
getStackTrace: [Function],
setStackTrace: [Function],
addSuppressed: [Function],
getSuppressed: [Function]
printStackTrace
,例如,打印 java 堆栈跟踪:
try {
Java.type('ClassWithException').throwRuntimeEx();
} catch (e) {
console.log(e.printStackTrace())
}
给出以下内容:
java.lang.RuntimeException: Example message
at ex.ClassWithException.throwRuntimeEx(ClassWithException.java:6)
at com.oracle.truffle.polyglot.HostMethodDesc$SingleMethod$MHBase.invokeHandle(HostMethodDesc.java:269)
at com.oracle.truffle.polyglot.HostMethodDesc$SingleMethod$MHBase.invoke(HostMethodDesc.java:261)
at com.oracle.truffle.polyglot.HostExecuteNode$1.executeImpl(HostExecuteNode.java:776)
at com.oracle.truffle.polyglot.GuestToHostRootNode.execute(GuestToHostRootNode.java:87)
at org.graalvm.compiler.truffle.runtime.OptimizedCallTarget.callProxy(OptimizedCallTarget.java:328)
...
at com.oracle.truffle.polyglot.PolyglotValue$InteropValue.execute(PolyglotValue.java:2008)
at org.graalvm.polyglot.Value.execute(Value.java:338)
at com.oracle.truffle.trufflenode.GraalJSAccess.isolateEnterPolyglotEngine(GraalJSAccess.java:2629)
Caused by: java.lang.RuntimeException: Inner exception
... 245 more
推荐阅读
- mysql - 查询得到我不明白的结果
- python - 使用 Python 中的 NewsPaper 库将多个新闻文章源抓取到一个列表中?
- flutter - 焦点更改时的 TextFormField 验证
- java - 已部署的 Java 应用程序在使用 32 位库的 Windows 7 64 位操作系统上显示“不是有效的 32 位应用程序”
- ios - iOS TimeZone 获取三个字母的缩写
- mysql - 插入序数
- spring-boot - 同步 S3 存储桶并监听变化
- python - 使用 numpy 的欧几里得距离
- android - 当我尝试将一些数据放入房间数据库时收到错误消息
- omnet++ - 在 OMNET++ 中构建 OpenFlow 项目时出错