private String invokeTemplate(Invocable templateInvocable, String code, Config params) throws TemplateException { String context; try { context = jsonMapper.writeValueAsString(params); } catch (RuntimeException | IOException ex) { throw new TemplateException("Failed to serialize parameters to JSON", ex); } try { return (String) templateInvocable.invokeFunction("template", code, context); } catch (ScriptException ex) { String message; if (ex.getCause() != null) { // ScriptException.getMessage includes filename and line number but they // are confusing because filename is always dummy file name and line number // is not accurate. message = ex.getCause().getMessage(); } else { message = ex.getMessage(); } throw new TemplateException("Failed to evaluate a variable " + code + " (" + message + ")"); } catch (NoSuchMethodException ex) { throw new TemplateException("Failed to evaluate JavaScript code: " + code, ex); } }
private TableSchema tableSchema(Config params) { try { return params.get("schema", TableSchema.class); } catch (ConfigException ignore) { } String fileName = params.get("schema", String.class); try { String schemaString = workspace.templateFile(templateEngine, fileName, UTF_8, params); if (FilenameUtils.getExtension(fileName).equals("json")) { return objectMapper.readValue(schemaString, TableSchema.class); } else { ObjectNode schemaJson = new YamlLoader().loadString(schemaString); return objectMapper.readValue(schemaJson.traverse(), TableSchema.class); } } catch (IOException ex) { throw workspace.propagateIoException(ex, fileName, ConfigException::new); } catch (TemplateException ex) { throw new ConfigException( String.format(ENGLISH, "%s in %s", ex.getMessage(), fileName), ex); } } }
throw new ConfigException(ex.getMessage(), ex);