Changed MustacheContextException -> MustacheException.Context.

Also made data into final fields. I don't like unnecessary getters.
This commit is contained in:
Michael Bayne
2013-03-05 20:55:01 -08:00
parent c61650734d
commit 43db03a26a
4 changed files with 26 additions and 37 deletions
@@ -609,8 +609,8 @@ public class Mustache
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
Object value = tmpl.getValueOrDefault(ctx, _name, _line);
if (value == null) {
throw new MustacheContextException(
"No key, method or field with name '" + _name + "' on line " + _line, _name, _line);
throw new MustacheException.Context("No key, method or field with name '" + _name +
"' on line " + _line, _name, _line);
}
String text = String.valueOf(value);
write(out, _escapeHTML ? escapeHTML(text) : text);
@@ -1,32 +0,0 @@
package com.samskivert.mustache;
/**
* An exception thrown if we encounter an unsatisfactory context error (e.g. a missing variable) while executing a template.
*/
public class MustacheContextException extends MustacheException {
private String key;
private int lineNo;
public MustacheContextException (String message, String key, int lineNo) {
super(message);
this.key = key;
this.lineNo = lineNo;
}
public MustacheContextException (String message, String key, int lineNo, Throwable cause) {
super(message, cause);
this.key = key;
this.lineNo = lineNo;
}
/** Get key (e.g. variable or section name) that caused the problem. */
public String getKey() {
return key;
}
/** Line number where the problem occurred. */
public int getLineNo() {
return lineNo;
}
}
@@ -9,6 +9,27 @@ package com.samskivert.mustache;
*/
public class MustacheException extends RuntimeException
{
/** An exception thrown if we encounter a context error (e.g. a missing variable) while
* compiling or executing a template. */
public static class Context extends MustacheException {
/** The key that caused the problem. */
public final String key;
/** The line number of the template on which the problem occurred. */
public final int lineNo;
public Context (String message, String key, int lineNo) {
super(message);
this.key = key;
this.lineNo = lineNo;
}
public Context (String message, String key, int lineNo, Throwable cause) {
super(message, cause);
this.key = key;
this.lineNo = lineNo;
}
}
public MustacheException (String message) {
super(message);
@@ -134,7 +134,7 @@ public class Template
Object data = getValue(ctx, comps[0].intern(), line, missingIsNull);
for (int ii = 1; ii < comps.length; ii++) {
if (data == NO_FETCHER_FOUND) {
if (!missingIsNull) throw new MustacheContextException(
if (!missingIsNull) throw new MustacheException.Context(
"Missing context for compound variable '" + name + "' on line " + line +
". '" + comps[ii - 1] + "' was not found.", name, line);
return null;
@@ -231,7 +231,7 @@ public class Template
_fcache.put(key, fetcher);
return value;
} catch (Exception e) {
throw new MustacheContextException(
throw new MustacheException.Context(
"Failure fetching variable '" + name + "' on line " + line, name, line, e);
}
}
@@ -240,7 +240,7 @@ public class Template
{
if (value == NO_FETCHER_FOUND) {
if (missingIsNull) return null;
throw new MustacheContextException(
throw new MustacheException.Context(
"No method or field with name '" + name + "' on line " + line, name, line);
} else {
return value;