private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException { // These are calculated from transient fields, so this is the last chance to calculate them: getFTLInstructionStack(); getFTLInstructionStackTopFew(); getDescription(); calculatePosition(); getBlamedExpressionString(); out.defaultWriteObject(); }
private String renderTemplate(String configurationId, Map<String, Object> sidecarContext) throws RenderTemplateException { Writer writer = new StringWriter(); String template; final Map<String, Object> context = new HashMap<>(); context.put("sidecar", sidecarContext); final Map<String, Object> userContext = configurationVariableService.all().stream().collect(Collectors.toMap(ConfigurationVariable::name, ConfigurationVariable::content)); context.put(ConfigurationVariable.VARIABLE_PREFIX, userContext); try { Template compiledTemplate = templateConfiguration.getTemplate(configurationId); compiledTemplate.process(context, writer); } catch (TemplateException e) { LOG.error("Failed to render template: " + e.getMessageWithoutStackTop()); throw new RenderTemplateException(e.getFTLInstructionStack(), e); } catch (IOException e) { LOG.error("Failed to render template: ", e); throw new RenderTemplateException(e.getMessage(), e); } template = writer.toString(); return template.endsWith("\n") ? template : template + "\n"; } }
String stackTrace = getFTLInstructionStack(); if (stackTrace != null) {
private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException { // These are calculated from transient fields, so this is the last chance to calculate them: getFTLInstructionStack(); getFTLInstructionStackTopFew(); getDescription(); calculatePosition(); getBlamedExpressionString(); out.defaultWriteObject(); }
@Override public void handleTemplateException(TemplateException templateexception, Environment environment, Writer writer) throws TemplateException { try { String code = templateexception.getFTLInstructionStack(); if (null != code && code.indexOf("Failed at: ") > 0 && code.indexOf(" [") > 0) { writer.write("error:" + code.substring(code.indexOf("Failed at: ") + 11, code.indexOf(" ["))); } else { writer.write("[some errors occurred!]"); } } catch (IOException e) { log.error(environment.getCurrentTemplate().getSourceName(), e); } }
private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException { // These are calculated from transient fields, so this is the last chance to calculate them: getFTLInstructionStack(); getFTLInstructionStackTopFew(); getDescription(); calculatePosition(); getBlamedExpressionString(); out.defaultWriteObject(); }
@Override public void handleTemplateException(TemplateException templateexception, Environment environment, Writer writer) throws TemplateException { try { String code = templateexception.getFTLInstructionStack(); if (null != code && code.indexOf("Failed at: ") > 0 && code.indexOf(" [") > 0) { writer.write("error:" + code.substring(code.indexOf("Failed at: ") + 11, code.indexOf(" ["))); } else { writer.write("[some errors occurred!]"); } } catch (IOException e) { log.error(environment.getCurrentTemplate().getSourceName(), e); } }
cfg.setTemplateExceptionHandler(new TemplateExceptionHandler() { @Override public void handleTemplateException(TemplateException te, Environment e, Writer writer) throws TemplateException { try { String errorString = te.getFTLInstructionStack(); int startIndex = errorString.indexOf('$'); if (startIndex != -1) { int endIndex = errorString.indexOf('}', startIndex); if (endIndex != -1) { writer.write(errorString.substring(startIndex, endIndex + 1)); } else { throw te; } } else { throw te; } } catch (IOException ex) { ex.printStackTrace(); } } });
@Override public void handleTemplateException(TemplateException templateexception, Environment environment, Writer writer) throws TemplateException { try { String code = templateexception.getFTLInstructionStack(); if (null != code && code.indexOf("Failed at: ") > 0 && code.indexOf(" [") > 0) { String xx = code.substring(code.indexOf("Failed at: ") + 11, code.indexOf(" [")); writer.write(xx); // log.error("freemarker error :{}", xx); } else { writer.write("[some errors occurred!]"); } } catch (IOException e) { log.error(environment.getCurrentTemplate().getSourceName(), e); } }
public static void processTemplate(String fullConfig, Writer out, boolean strict, boolean verbose, HashMap<String, Object> hashMap) throws IOException, TemplateException { Configuration cfg = new Configuration(new Version(2, 3, 0)); cfg.setLogTemplateExceptions(false); TemplateLoader templateLoader = new FileTemplateLoader(); cfg.setTemplateExceptionHandler((e, env, output) -> { String replace = e.getFTLInstructionStack().replace("\n", "").replace("\r", "").replace("\t", ""); if (strict) throw new RuntimeException("Error during processing template: " + replace + (verbose ? " " + e.getMessage() : ""), e); else System.out.println("Warning:" + replace + (verbose ? " " + e.getMessage() : "")); }); Template template = new Template(fullConfig, templateLoader.getReader(fullConfig, ENCODING), cfg); template.process(hashMap, out); }
/** * {@inheritDoc} */ @Override public HttpResponse render(String name, Object... keyOrVals) { TemplatedHttpResponse response = TemplatedHttpResponse.create(name, keyOrVals); response.setBody(new LazyRenderInputStream(() -> { try { Template template = config.getTemplate(name + suffix, encoding); StringWriter writer = new StringWriter(); template.process(response.getContext(), writer); return new ByteArrayInputStream(writer.toString().getBytes(encoding)); } catch (TemplateException e) { throw new MisconfigurationException("freemarker.TEMPLATE", e.getFTLInstructionStack(), e.getMessageWithoutStackTop(), e); } catch (IOException e) { throw new MisconfigurationException("freemarker.TEMPLATE", e.getMessage(), String.format(Locale.US, "Make a template '%s'.", name), e); } })); HttpResponseUtils.contentType(response, "text/html"); return response; }
public void merge(Map<String, Object> context, StringWriter writer) throws TemplateMergeException { try { template.process(context, writer); template.setTemplateExceptionHandler(new TemplateExceptionHandler() { @Override public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException { te.printStackTrace(); } }); } catch (TemplateException templateException) { LOGGER.error("Syntax error in report template: {}\n{}", templateException.getMessage(), templateException.getFTLInstructionStack()); throw new TemplateMergeException("Failed to process FreeMarker template", templateException); } catch (IOException e) { throw new TemplateMergeException("Could not read FreeMarker template", e); } catch (NumberFormatException e) { throw new TemplateMergeException("Number format exception during template merge", e); } } }
public void merge(Map<String, Object> context, StringWriter writer) throws TemplateMergeException { try { template.process(context, writer); template.setTemplateExceptionHandler(new TemplateExceptionHandler() { @Override public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException { te.printStackTrace(); } }); } catch (TemplateException templateException) { LOGGER.error("Syntax error in report template: {}\n{}", templateException.getMessage(), templateException.getFTLInstructionStack()); throw new TemplateMergeException("Failed to process FreeMarker template", templateException); } catch (IOException e) { throw new TemplateMergeException("Could not read FreeMarker template", e); } catch (NumberFormatException e) { throw new TemplateMergeException("Number format exception during template merge", e); } } }
public void merge(Map<String, Object> context, Writer writer) throws TemplateMergeException { try { Environment environment = template.createProcessingEnvironment(context, writer); environment.setOutputEncoding(StandardCharsets.UTF_8.name()); environment.setTemplateExceptionHandler(new TemplateExceptionHandler() { @Override public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException { LOGGER.warn("Report generation failed", te); throw te; } }); environment.process(); } catch (TemplateException templateException) { LOGGER.error("Syntax error in report template: {}\n{}", templateException.getMessage(), templateException.getFTLInstructionStack()); throw new TemplateMergeException("Failed to process FreeMarker template", templateException); } catch (IOException e) { throw new TemplateMergeException("Could not read FreeMarker template", e); } catch (NumberFormatException e) { throw new TemplateMergeException("Number format exception during template merge", e); } } }
String stackTrace = getFTLInstructionStack(); if (stackTrace != null) {
String stackTrace = getFTLInstructionStack(); if (stackTrace != null) {