use of alfio.extension.exception.AlfioScriptingException in project alf.io by alfio-event.
the class ScriptingExecutionService method executeScriptFinally.
@SuppressWarnings("unchecked")
private <T> T executeScriptFinally(String name, String script, Map<String, Object> params, Class<T> clazz, ExtensionLogger extensionLogger) {
Context cx = Context.enter();
try {
if (params == null) {
params = Collections.emptyMap();
}
Scriptable scope = cx.newObject(sealedScope);
scope.setPrototype(sealedScope);
scope.setParentScope(null);
scope.put("extensionLogger", scope, extensionLogger);
scope.put("console", scope, new ConsoleLogger(extensionLogger));
// retrocompatibility
scope.put("Java", scope, new JavaClassInterop(Map.of("alfio.model.CustomerName", alfio.model.CustomerName.class), scope));
scope.put("returnClass", scope, clazz);
for (var entry : params.entrySet()) {
var value = entry.getValue();
if (entry.getKey().equals(EXTENSION_CONFIGURATION_PARAMETERS)) {
scope.put(entry.getKey(), scope, convertExtensionParameters(scope, value));
} else {
scope.put(entry.getKey(), scope, Context.javaToJS(value, scope));
}
}
Object res;
res = cx.evaluateString(scope, script, name, 1, null);
extensionLogger.logSuccess("Script executed successfully.");
if (res instanceof NativeJavaObject) {
NativeJavaObject nativeRes = (NativeJavaObject) res;
return (T) nativeRes.unwrap();
} else if (clazz.isInstance(res)) {
return (T) res;
} else {
return null;
}
} catch (EcmaError ex) {
log.warn("Syntax error detected in script " + name, ex);
extensionLogger.logError("Syntax error while executing script: " + ex.getMessage() + "(" + ex.lineNumber() + ":" + ex.columnNumber() + ")");
throw new InvalidScriptException("Syntax error in script " + name);
} catch (WrappedException ex) {
var actualException = ex.getWrappedException();
var message = getErrorMessage(actualException);
extensionLogger.logError("Error from script: " + message);
throw new AlfioScriptingException(message, actualException);
} catch (JavaScriptException ex) {
String message;
if (ex.getValue() != null) {
message = ex.details();
} else {
message = ex.getMessage();
}
extensionLogger.logError(message);
throw new ScriptRuntimeException(message, ex);
} catch (OutOfBoundariesException ex) {
throw ex;
} catch (Exception ex) {
//
extensionLogger.logError("Error while executing script: " + ex.getMessage());
throw new IllegalStateException(ex);
} finally {
Context.exit();
}
}
Aggregations