2 * Copyright (c) 2010-2024 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
13 package org.openhab.automation.jsscripting.internal;
15 import javax.script.Invocable;
16 import javax.script.ScriptEngine;
18 import org.graalvm.polyglot.PolyglotException;
19 import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
26 * @author Jonathan Gilbert - Initial contribution
28 class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
29 extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
31 private static final Logger STACK_LOGGER = LoggerFactory
32 .getLogger("org.openhab.automation.script.javascript.stack");
34 public DebuggingGraalScriptEngine(T delegate) {
39 public Exception afterThrowsInvocation(Exception e) {
40 Throwable cause = e.getCause();
41 if (cause instanceof IllegalArgumentException) {
42 STACK_LOGGER.error("Failed to execute script:", e);
44 if (cause instanceof PolyglotException) {
45 STACK_LOGGER.error("Failed to execute script:", cause);