Skip to content

Commit 2d73ab2

Browse files
committed
Added support for logging when template not found
1 parent 67fcef3 commit 2d73ab2

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/com/googlecode/totallylazy/template/Templates.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.googlecode.totallylazy.functions.Function2;
55
import com.googlecode.totallylazy.io.Uri;
66
import com.googlecode.totallylazy.predicates.Predicate;
7-
import com.googlecode.totallylazy.predicates.Predicates;
87
import com.googlecode.totallylazy.xml.Xml;
98

109
import java.io.IOException;
@@ -16,6 +15,7 @@
1615
import static com.googlecode.totallylazy.Unchecked.cast;
1716
import static com.googlecode.totallylazy.predicates.Predicates.always;
1817
import static com.googlecode.totallylazy.template.CompositeRenderer.compositeRenderer;
18+
import static java.lang.String.format;
1919

2020
public class Templates implements Renderers {
2121
private final Function2<? super String, ? super Renderers, ? extends Renderer<?>> missing;
@@ -84,4 +84,15 @@ public Renderer<Object> get(String name) {
8484
public Templates extension(String value) {
8585
return new Templates((s, r) -> missing.apply(s + "." + value, r));
8686
}
87+
88+
public Templates logger(Appendable logger) {
89+
return new Templates((s, r) -> {
90+
try {
91+
return missing.apply(s, r);
92+
} catch (Exception e) {
93+
logger.append(format("Unable to load template '%s' because: %s", s, e.getMessage()));
94+
return (instance, appendable) -> appendable;
95+
}
96+
});
97+
}
8798
}

test/com/googlecode/totallylazy/template/TemplatesTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
import com.googlecode.totallylazy.Strings;
44
import org.junit.Test;
55

6+
import java.io.PrintWriter;
7+
68
import static com.googlecode.totallylazy.Assert.assertThat;
79
import static com.googlecode.totallylazy.Maps.map;
10+
import static com.googlecode.totallylazy.Strings.startsWith;
11+
import static com.googlecode.totallylazy.predicates.Predicates.contains;
812
import static com.googlecode.totallylazy.predicates.Predicates.instanceOf;
913
import static com.googlecode.totallylazy.predicates.Predicates.is;
1014
import static com.googlecode.totallylazy.template.Templates.templates;
@@ -50,4 +54,13 @@ public void canUseTemplatesFromClassPathAndDefaultTemplates() throws Exception {
5054
String result = template.render(map("name", "Dan"));
5155
assertThat(result, is("Say Hello Dan"));
5256
}
57+
58+
@Test
59+
public void doesNotThrowWhenASubTemplateIsNotFoundButReturnsEmptyStringAndLogs() throws Exception {
60+
StringBuilder log = new StringBuilder();
61+
Templates templates = templates(getClass()).logger(log).extension("st");
62+
String result = templates.get("error").render(map());
63+
assertThat(result, is("Sub template returned ''"));
64+
assertThat(log.toString(), startsWith("Unable to load template 'foo.st' because: "));
65+
}
5366
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sub template returned '$foo()$'

0 commit comments

Comments
 (0)