use of org.graalvm.polyglot.PolyglotException.StackFrame in project graal by oracle.
the class ProxyAPITest method testExceptionFrames.
@Test
public void testExceptionFrames() {
Value innerInner = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
throw new RuntimeException("foobar");
}
});
Value inner = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
return innerInner.execute();
}
});
Value outer = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
return inner.execute();
}
});
try {
outer.execute();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof RuntimeException);
assertEquals("foobar", e.getMessage());
Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
StackFrame frame;
for (int i = 0; i < 6; i++) {
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("execute", frame.toHostFrame().getMethodName());
}
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("testExceptionFrames", frame.toHostFrame().getMethodName());
}
}
use of org.graalvm.polyglot.PolyglotException.StackFrame in project graal by oracle.
the class ValueHostConversionTest method testExceptionFrames2.
@Test
public void testExceptionFrames2() {
Value value = context.asValue(new TestExceptionFrames2());
try {
value.getMember("foo").execute();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof RuntimeException);
assertEquals("foo", e.getMessage());
Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
StackFrame frame;
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("foo", frame.toHostFrame().getMethodName());
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("execute", frame.toHostFrame().getMethodName());
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("testExceptionFrames2", frame.toHostFrame().getMethodName());
}
}
use of org.graalvm.polyglot.PolyglotException.StackFrame in project graal by oracle.
the class PolyglotExceptionImpl method printStackTrace.
private void printStackTrace(PrintStreamOrWriter s) {
// using a Set with identity equality semantics.
synchronized (s.lock()) {
// Print our stack trace
if (isInternalError() || getMessage() == null || getMessage().isEmpty()) {
s.println(api);
} else {
s.println(getMessage());
}
materialize();
// java
int languageIdLength = 0;
for (StackFrame traceElement : getPolyglotStackTrace()) {
if (!traceElement.isHostFrame()) {
languageIdLength = Math.max(languageIdLength, getAPIAccess().getImpl(traceElement).getLanguage().getId().length());
}
}
for (StackFrame traceElement : getPolyglotStackTrace()) {
s.println("\tat " + getAPIAccess().getImpl(traceElement).toStringImpl(languageIdLength));
}
// Print cause, if any
if (isHostException()) {
s.println(CAUSE_CAPTION + asHostException());
}
if (isInternalError()) {
s.println("Original Internal Error: ");
s.printStackTrace(exception);
}
}
}
use of org.graalvm.polyglot.PolyglotException.StackFrame in project graal by oracle.
the class SLExceptionTest method assertGuestFrame.
private static void assertGuestFrame(Iterator<StackFrame> frames, String languageId, String rootName, String fileName, int charIndex, int endIndex) {
assertTrue(frames.hasNext());
StackFrame frame = frames.next();
assertTrue(frame.isGuestFrame());
assertEquals(languageId, frame.getLanguage().getId());
assertEquals(rootName, frame.getRootName());
assertNotNull(frame.getSourceLocation());
assertNotNull(frame.getSourceLocation().getSource());
assertEquals(fileName, frame.getSourceLocation().getSource().getName());
assertEquals(charIndex, frame.getSourceLocation().getCharIndex());
assertEquals(endIndex, frame.getSourceLocation().getCharEndIndex());
StackTraceElement hostFrame = frame.toHostFrame();
assertEquals("<" + languageId + ">", hostFrame.getClassName());
assertEquals(rootName, hostFrame.getMethodName());
assertEquals(frame.getSourceLocation().getStartLine(), hostFrame.getLineNumber());
assertEquals(fileName, hostFrame.getFileName());
assertNotNull(hostFrame.toString());
assertTrue(hostFrame.equals(hostFrame));
assertNotEquals(0, hostFrame.hashCode());
}
use of org.graalvm.polyglot.PolyglotException.StackFrame in project graal by oracle.
the class SLExceptionTest method assertFrames.
private static void assertFrames(boolean isEval, PolyglotException e, String... expectedFrames) {
int i = 0;
boolean firstHostFrame = false;
// Expected exception
for (StackFrame frame : e.getPolyglotStackTrace()) {
if (i < expectedFrames.length && expectedFrames[i] != null) {
Assert.assertTrue(frame.isGuestFrame());
Assert.assertEquals("sl", frame.getLanguage().getId());
Assert.assertEquals(expectedFrames[i], frame.getRootName());
Assert.assertTrue(frame.getSourceLocation() != null);
firstHostFrame = true;
} else {
Assert.assertTrue(frame.isHostFrame());
if (firstHostFrame) {
Assert.assertEquals(isEval ? "org.graalvm.polyglot.Context.eval" : "org.graalvm.polyglot.Value.execute", frame.getRootName());
firstHostFrame = false;
}
}
i++;
}
}
Aggregations