Search in sources :

Example 1 with TruffleException

use of com.oracle.truffle.api.TruffleException in project graal by oracle.

the class TruffleExecutionContext method executeInSuspendThread.

<T> T executeInSuspendThread(SuspendThreadExecutable<T> executable) throws NoSuspendedThreadException, GuestLanguageException, CommandProcessException {
    CompletableFuture<T> cf = new CompletableFuture<>();
    suspendThreadExecutor.execute(new CancellableRunnable() {

        @Override
        public void run() {
            T params = null;
            try {
                params = executable.executeCommand();
                cf.complete(params);
            } catch (ThreadDeath td) {
                cf.completeExceptionally(td);
                throw td;
            } catch (Throwable t) {
                cf.completeExceptionally(t);
            }
        }

        @Override
        public void cancel() {
            cf.completeExceptionally(new NoSuspendedThreadException("Resuming..."));
        }
    });
    T params;
    try {
        params = cf.get();
    } catch (ExecutionException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof TruffleException && !((TruffleException) cause).isInternalError()) {
            throw new GuestLanguageException((TruffleException) cause);
        }
        if (cause instanceof CommandProcessException) {
            throw (CommandProcessException) cause;
        }
        if (cause instanceof NoSuspendedThreadException) {
            throw (NoSuspendedThreadException) cause;
        }
        if (err != null) {
            cause.printStackTrace(err);
        }
        throw new CommandProcessException(ex.getLocalizedMessage());
    } catch (InterruptedException ex) {
        throw new CommandProcessException(ex.getLocalizedMessage());
    }
    return params;
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException) TruffleException(com.oracle.truffle.api.TruffleException)

Example 2 with TruffleException

use of com.oracle.truffle.api.TruffleException in project graal by oracle.

the class PolyglotExceptionTest method testLanguageExceptionUnwrapping.

@Test
public void testLanguageExceptionUnwrapping() {
    Context context = Context.create();
    Value throwError = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            throw new RuntimeException();
        }
    });
    VerifyErrorTruffleObject checkErrorObj = new VerifyErrorTruffleObject();
    Value checkError = context.asValue(checkErrorObj);
    checkErrorObj.verifyError = (e) -> {
        Assert.assertTrue(e instanceof TruffleException);
        Assert.assertEquals("HostException", e.getClass().getSimpleName());
    };
    Assert.assertTrue(checkError.execute(throwError).asBoolean());
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleException(com.oracle.truffle.api.TruffleException) Test(org.junit.Test)

Example 3 with TruffleException

use of com.oracle.truffle.api.TruffleException in project graal by oracle.

the class TruffleDebugger method evaluateOnCallFrame.

@Override
public Params evaluateOnCallFrame(String callFrameId, String expression, String objectGroup, boolean includeCommandLineAPI, boolean silent, boolean returnByValue, boolean generatePreview, boolean throwOnSideEffect) throws CommandProcessException {
    if (callFrameId == null) {
        throw new CommandProcessException("A callFrameId required.");
    }
    if (expression == null) {
        throw new CommandProcessException("An expression required.");
    }
    int frameId;
    try {
        frameId = Integer.parseInt(callFrameId);
    } catch (NumberFormatException ex) {
        throw new CommandProcessException(ex.getLocalizedMessage());
    }
    JSONObject jsonResult;
    try {
        jsonResult = context.executeInSuspendThread(new SuspendThreadExecutable<JSONObject>() {

            @Override
            public JSONObject executeCommand() throws CommandProcessException {
                if (frameId >= suspendedInfo.getCallFrames().length) {
                    throw new CommandProcessException("Too big callFrameId: " + frameId);
                }
                CallFrame cf = suspendedInfo.getCallFrames()[frameId];
                JSONObject json = new JSONObject();
                try {
                    DebugValue value = cf.getFrame().eval(expression);
                    RemoteObject ro = new RemoteObject(value, context.getErr());
                    context.getRemoteObjectsHandler().register(ro);
                    json.put("result", ro.toJSON());
                } catch (Throwable t) {
                    if (t instanceof TruffleException && !((TruffleException) t).isInternalError()) {
                        JSONObject err = new JSONObject();
                        err.putOpt("value", t.getLocalizedMessage());
                        json.put("result", err);
                    } else {
                        throw t;
                    }
                }
                return json;
            }
        });
    } catch (NoSuspendedThreadException e) {
        jsonResult = new JSONObject();
        JSONObject err = new JSONObject();
        err.putOpt("value", e.getLocalizedMessage());
        jsonResult.put("result", err);
    } catch (GuestLanguageException e) {
        jsonResult = new JSONObject();
        TruffleRuntime.fillExceptionDetails(jsonResult, e);
    }
    return new Params(jsonResult);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) DebugValue(com.oracle.truffle.api.debug.DebugValue) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.NoSuspendedThreadException) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) RemoteObject(com.oracle.truffle.tools.chromeinspector.types.RemoteObject) JSONObject(org.json.JSONObject) CallFrame(com.oracle.truffle.tools.chromeinspector.types.CallFrame) GuestLanguageException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.GuestLanguageException) TruffleException(com.oracle.truffle.api.TruffleException)

Aggregations

TruffleException (com.oracle.truffle.api.TruffleException)3 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)2 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)1 DebugValue (com.oracle.truffle.api.debug.DebugValue)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 GuestLanguageException (com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.GuestLanguageException)1 NoSuspendedThreadException (com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.NoSuspendedThreadException)1 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)1 CallFrame (com.oracle.truffle.tools.chromeinspector.types.CallFrame)1 RemoteObject (com.oracle.truffle.tools.chromeinspector.types.RemoteObject)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Context (org.graalvm.polyglot.Context)1 Value (org.graalvm.polyglot.Value)1 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1