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