Search in sources :

Example 1 with StackFrame

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());
    }
}
Also used : ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Iterator(java.util.Iterator) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 2 with StackFrame

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());
    }
}
Also used : StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Iterator(java.util.Iterator) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 3 with StackFrame

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);
        }
    }
}
Also used : StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame)

Example 4 with StackFrame

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());
}
Also used : StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame)

Example 5 with StackFrame

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++;
    }
}
Also used : StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame)

Aggregations

StackFrame (org.graalvm.polyglot.PolyglotException.StackFrame)9 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)4 Iterator (java.util.Iterator)4 PolyglotException (org.graalvm.polyglot.PolyglotException)4 Value (org.graalvm.polyglot.Value)4 Test (org.junit.Test)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 ArrayList (java.util.ArrayList)1 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)1 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)1