]> git.basschouten.com Git - openhab-addons.git/blob
f66b5f9ca1ccd622857a70dd6e2697e7ce330cc1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jsscripting.internal;
14
15 import javax.script.Invocable;
16 import javax.script.ScriptEngine;
17
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;
22
23 /**
24  * Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
25  *
26  * @author Jonathan Gilbert - Initial contribution
27  */
28 class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
29         extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
30
31     private static final Logger STACK_LOGGER = LoggerFactory
32             .getLogger("org.openhab.automation.script.javascript.stack");
33
34     public DebuggingGraalScriptEngine(T delegate) {
35         super(delegate);
36     }
37
38     @Override
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);
43         }
44         if (cause instanceof PolyglotException) {
45             STACK_LOGGER.error("Failed to execute script:", cause);
46         }
47         return e;
48     }
49 }