From c61650734db03768b5c5eaebf017258338d7a728 Mon Sep 17 00:00:00 2001 From: pmenhart Date: Tue, 5 Mar 2013 23:33:48 -0500 Subject: [PATCH] Added MustacheContextException Solving need to distinguish context errors from environment errors. The exception has fields for the offending key and line number. This is a first step towards internationalization of messages. --- .../com/samskivert/mustache/Mustache.java | 4 +-- .../mustache/MustacheContextException.java | 32 +++++++++++++++++++ .../mustache/MustacheException.java | 5 --- .../com/samskivert/mustache/Template.java | 12 +++---- 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/samskivert/mustache/MustacheContextException.java diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index 37e5cc6..c39192f 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 MustacheException( - "No key, method or field with name '" + _name + "' on line " + _line); + throw new MustacheContextException( + "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 new file mode 100644 index 0000000..a4a6c7a --- /dev/null +++ b/src/main/java/com/samskivert/mustache/MustacheContextException.java @@ -0,0 +1,32 @@ +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 93d31fe..7b3b827 100644 --- a/src/main/java/com/samskivert/mustache/MustacheException.java +++ b/src/main/java/com/samskivert/mustache/MustacheException.java @@ -9,11 +9,6 @@ package com.samskivert.mustache; */ public class MustacheException extends RuntimeException { - public static class Parse extends MustacheException { - public Parse (String message) { - super(message); - } - } 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 86123ff..bb4e33c 100644 --- a/src/main/java/com/samskivert/mustache/Template.java +++ b/src/main/java/com/samskivert/mustache/Template.java @@ -134,9 +134,9 @@ 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 MustacheException( + if (!missingIsNull) throw new MustacheContextException( "Missing context for compound variable '" + name + "' on line " + line + - ". '" + comps[ii - 1] + "' was not found."); + ". '" + comps[ii - 1] + "' was not found.", name, line); return null; } else if (data == null) { return null; @@ -231,8 +231,8 @@ public class Template _fcache.put(key, fetcher); return value; } catch (Exception e) { - throw new MustacheException( - "Failure fetching variable '" + name + "' on line " + line, e); + throw new MustacheContextException( + "Failure fetching variable '" + name + "' on line " + line, name, line, e); } } @@ -240,8 +240,8 @@ public class Template { if (value == NO_FETCHER_FOUND) { if (missingIsNull) return null; - throw new MustacheException( - "No method or field with name '" + name + "' on line " + line); + throw new MustacheContextException( + "No method or field with name '" + name + "' on line " + line, name, line); } else { return value; }