Skip to content
Permalink
Browse files

Improve error reporting in `JMustacheRenderer`

See #72
  • Loading branch information...
testinfected committed Mar 2, 2018
1 parent 3766358 commit 14689148388d295f5edb55fc988f78aef2cf33dc
Showing with 11 additions and 5 deletions.
  1. +11 −5 src/main/java/com/vtence/molecule/templating/JMustacheRenderer.java
@@ -56,11 +56,17 @@ public JMustacheRenderer nullValue(String nullValue) {
}

public void render(Writer out, String templateName, Object context) throws IOException {
try (Reader source = mustache.loader.getTemplate(templateName)) {
try (Reader source = load(templateName)) {
Template template = mustache.compile(source);
template.execute(context, out);
}
}

private Reader load(String templateName) throws IOException {
try {
return mustache.loader.getTemplate(templateName);
} catch (Exception e) {
throw new IOException("Unable to load template " + templateName, e);
throw new IOException("loading template `" + templateName + "`", e);
}
}

@@ -95,7 +101,7 @@ public ClasspathTemplateLoader(String root) {
}

public static ClasspathTemplateLoader classpath() {
return classpath(".");
return classpath("");
}

public static ClasspathTemplateLoader classpath(String root) {
@@ -108,9 +114,9 @@ public ClasspathTemplateLoader usingClassLoader(ClassLoader classLoader) {
}

public Reader getTemplate(String name) throws IOException {
String resource = root + "/" + name + "." + extension;
String resource = (root.equals("") ? "" : root + "/") + name + "." + extension;
URL location = classLoader.getResource(resource);
if (location == null) throw new FileNotFoundException("Cannot find resource: " + resource);
if (location == null) throw new FileNotFoundException("classpath:" + resource);
return new InputStreamReader(location.openStream(), encoding);
}
}

0 comments on commit 1468914

Please sign in to comment.
You can’t perform that action at this time.