diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index c39192f..77cb784 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -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); diff --git a/src/main/java/com/samskivert/mustache/MustacheContextException.java b/src/main/java/com/samskivert/mustache/MustacheContextException.java deleted file mode 100644 index a4a6c7a..0000000 --- a/src/main/java/com/samskivert/mustache/MustacheContextException.java +++ /dev/null @@ -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; - } - -} diff --git a/src/main/java/com/samskivert/mustache/MustacheException.java b/src/main/java/com/samskivert/mustache/MustacheException.java index 7b3b827..87956d6 100644 --- a/src/main/java/com/samskivert/mustache/MustacheException.java +++ b/src/main/java/com/samskivert/mustache/MustacheException.java @@ -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); diff --git a/src/main/java/com/samskivert/mustache/Template.java b/src/main/java/com/samskivert/mustache/Template.java index bb4e33c..d09a36c 100644 --- a/src/main/java/com/samskivert/mustache/Template.java +++ b/src/main/java/com/samskivert/mustache/Template.java @@ -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;