2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
14 package org.openhab.automation.jsscripting.internal;
16 import javax.script.Invocable;
17 import javax.script.ScriptEngine;
18 import javax.script.ScriptException;
20 import org.graalvm.polyglot.PolyglotException;
21 import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
28 * @author Jonathan Gilbert - Initial contribution
30 class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable>
31 extends InvocationInterceptingScriptEngineWithInvocable<T> {
33 private static final Logger STACK_LOGGER = LoggerFactory
34 .getLogger("org.openhab.automation.script.javascript.stack");
36 public DebuggingGraalScriptEngine(T delegate) {
41 public ScriptException afterThrowsInvocation(ScriptException se) {
42 Throwable cause = se.getCause();
43 if (cause instanceof PolyglotException) {
44 STACK_LOGGER.error("Failed to execute script:", cause);