Skip to content

Commit 334801b

Browse files
committed
Merge pull request #103 from rustyx/master
Improved reflection error handling
2 parents 3c72547 + 1fc28fb commit 334801b

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

compiler/src/main/java/com/github/mustachejava/MustacheException.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Generally there is nothing you can do if it fails.
55
*/
66
public class MustacheException extends RuntimeException {
7+
private TemplateContext context;
8+
79
public MustacheException() {
810
super();
911
}
@@ -16,7 +18,23 @@ public MustacheException(String s, Throwable throwable) {
1618
super(s, throwable);
1719
}
1820

21+
public MustacheException(String s, Throwable throwable, TemplateContext context) {
22+
super(s, throwable);
23+
this.context = context;
24+
}
25+
1926
public MustacheException(Throwable throwable) {
2027
super(throwable);
2128
}
29+
30+
@Override
31+
public String getMessage() {
32+
return context == null ? super.getMessage() : super.getMessage() + " @" + context;
33+
}
34+
35+
public void setContext(TemplateContext context) {
36+
if (this.context == null)
37+
this.context = context;
38+
}
39+
2240
}

compiler/src/main/java/com/github/mustachejava/codes/DefaultCode.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ public Object get(Object[] scopes) {
137137
}
138138
try {
139139
return binding.get(scopes);
140-
} catch (Exception e) {
141-
throw new MustacheException(e.getMessage() + "@" + tc.toString(), e);
142-
} catch (Error e) {
143-
throw new MustacheException(e.getMessage() + "@" + tc.toString(), e);
140+
} catch (MustacheException e) {
141+
e.setContext(tc);
142+
throw e;
143+
} catch (Throwable e) {
144+
throw new MustacheException(e.getMessage(), e, tc);
144145
}
145146
}
146147

compiler/src/main/java/com/github/mustachejava/codes/ValueCode.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ public ValueCode(TemplateContext tc, DefaultMustacheFactory df, String variable,
5757

5858
@Override
5959
public Writer execute(Writer writer, final Object[] scopes) {
60-
final Object object = get(scopes);
61-
if (object != null) {
62-
try {
60+
try {
61+
final Object object = get(scopes);
62+
if (object != null) {
6363
if (object instanceof Function) {
6464
handleFunction(writer, (Function) object, scopes);
6565
} else if (object instanceof Callable) {
6666
return handleCallable(writer, (Callable) object, scopes);
6767
} else {
6868
execute(writer, oh.stringify(object));
6969
}
70-
} catch (Exception e) {
71-
throw new MustacheException("Failed to get value for " + name + " at line " + tc.file() + ":" + tc.line(), e);
7270
}
71+
return super.execute(writer, scopes);
72+
} catch (Exception e) {
73+
throw new MustacheException("Failed to get value for " + name, e, tc);
7374
}
74-
return super.execute(writer, scopes);
7575
}
7676

7777
protected Writer handleCallable(Writer writer, final Callable callable, final Object[] scopes) throws Exception {

compiler/src/main/java/com/github/mustachejava/reflect/ReflectionWrapper.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.lang.reflect.Field;
1010
import java.lang.reflect.InvocationTargetException;
1111
import java.lang.reflect.Method;
12+
import java.util.Arrays;
1213

1314
/**
1415
* Used for evaluating values at a callsite
@@ -63,11 +64,15 @@ public Object call(Object[] scopes) throws GuardException {
6364
return method.invoke(scope, arguments);
6465
}
6566
} catch (IllegalArgumentException e) {
66-
throw new MustacheException("Wrong method for object: " + method + " on " + scope, e);
67-
} catch (InvocationTargetException e) {
68-
throw new MustacheException("Failed to execute method: " + method, e.getTargetException());
67+
throw new MustacheException("Error accessing " + getTargetDescription() + " on " + elementToString(scope)
68+
+ ", scope: [" + elementsToString(scopes, scopeIndex) + "]" + ", guards: " + Arrays.toString(guards), e);
6969
} catch (IllegalAccessException e) {
70-
throw new MustacheException("Failed to execute method: " + method, e);
70+
throw new MustacheException("Error accessing " + getTargetDescription() + " on " + elementToString(scope)
71+
+ ", scope: [" + elementsToString(scopes, scopeIndex) + "]" + ", guards: " + Arrays.toString(guards), e);
72+
} catch (InvocationTargetException e) {
73+
throw new MustacheException("Error invoking " + getTargetDescription() + " on " + elementToString(scope), e.getTargetException());
74+
} catch (Exception e) {
75+
throw new MustacheException("Error invoking " + getTargetDescription() + " on " + elementToString(scope), e);
7176
}
7277
}
7378

@@ -102,4 +107,28 @@ public String toString() {
102107
public Wrapper[] getWrappers() {
103108
return wrappers;
104109
}
110+
111+
private String getTargetDescription() {
112+
return method == null
113+
? "field " + field.getDeclaringClass() + "." + field.getName()
114+
: "method " + method.getDeclaringClass().getCanonicalName() + "." + method.getName() + "(" + elementsToString(arguments, method.getParameterTypes().length - 1) + ")";
115+
}
116+
117+
private String elementsToString(Object[] objects, int showUpTo) {
118+
if (objects == null || objects.length == 0 || showUpTo < 0) {
119+
return "";
120+
}
121+
StringBuilder sb = new StringBuilder();
122+
for (int i = 0; i <= showUpTo && i < objects.length; i++) {
123+
if (sb.length() > 0)
124+
sb.append(",");
125+
sb.append(elementToString(objects[i]));
126+
}
127+
return sb.toString();
128+
}
129+
130+
private String elementToString(Object object) {
131+
return object == null ? null : object.getClass().getCanonicalName() + '@' + object.hashCode();
132+
}
133+
105134
}

compiler/src/test/java/com/github/mustachejava/FailOnMissingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected synchronized Wrapper getWrapper(String name, Object[] scopes) {
4343
test.execute(new StringWriter(), new Object());
4444
fail("Should have failed");
4545
} catch (MustacheException me) {
46-
assertEquals("test not found in [test:1]@[test:1]", me.getMessage());
46+
assertEquals("test not found in [test:1] @[test:1]", me.getCause().getMessage());
4747
} catch (IOException e) {
4848
e.printStackTrace();
4949
}

0 commit comments

Comments
 (0)