]> git.basschouten.com Git - openhab-addons.git/blob
b53b70a0cf72e8979f2903d2c9a3385f5b9acaf5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
14 package org.openhab.automation.jsscripting.internal;
15
16 import javax.script.Invocable;
17 import javax.script.ScriptEngine;
18
19 import org.graalvm.polyglot.PolyglotException;
20 import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
26  *
27  * @author Jonathan Gilbert - Initial contribution
28  */
29 class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
30         extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
31
32     private static final Logger STACK_LOGGER = LoggerFactory
33             .getLogger("org.openhab.automation.script.javascript.stack");
34
35     public DebuggingGraalScriptEngine(T delegate) {
36         super(delegate);
37     }
38
39     @Override
40     public Exception afterThrowsInvocation(Exception e) {
41         Throwable cause = e.getCause();
42         if (cause instanceof IllegalArgumentException) {
43             STACK_LOGGER.error("Failed to execute script:", e);
44         }
45         if (cause instanceof PolyglotException) {
46             STACK_LOGGER.error("Failed to execute script:", cause);
47         }
48         return e;
49     }
50 }