From 2cdbd22290f9e84fe4abdd19c3913ae01af0e33e Mon Sep 17 00:00:00 2001 From: Dean Hiller Date: Thu, 11 Aug 2016 16:49:40 -0600 Subject: [PATCH] add information when a property is missing with file and line number --- .../templating/impl/source/ScriptWriter.java | 2 ++ .../templating/impl/source/TokenImpl.java | 3 ++- .../impl/GroovyTemplateSuperclass.java | 24 +++++++++++++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/ScriptWriter.java b/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/ScriptWriter.java index 246da60fa..d22924ff3 100644 --- a/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/ScriptWriter.java +++ b/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/ScriptWriter.java @@ -99,12 +99,14 @@ public void printScript() { public void printExpression(TokenImpl token, ScriptOutputImpl sourceCode) { String expr = token.getValue().trim(); + sourceCode.println( "enterExpression('"+token.getSourceLocation(false)+"');"); //purely so we can add info to missing properties if(expr.startsWith("_")) sourceCode.print(" __out.print("+expr+");"); else sourceCode.print(" __out.print(useFormatter("+expr+"));"); sourceCode.appendTokenComment(token); sourceCode.println(); + sourceCode.println( "exitExpression();"); } public void printMessage() { diff --git a/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/TokenImpl.java b/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/TokenImpl.java index b5ec62375..dac9891af 100644 --- a/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/TokenImpl.java +++ b/webserver/http-templating-dev/src/main/java/org/webpieces/templating/impl/source/TokenImpl.java @@ -1,6 +1,7 @@ package org.webpieces.templating.impl.source; import org.webpieces.templating.api.Token; +import org.webpieces.templating.impl.GroovyTemplateSuperclass; public class TokenImpl implements Token { @@ -51,7 +52,7 @@ public String getSourceLocation(boolean dueToError) { if(!dueToError) return loc; - return "\n\t"+loc+"\n"; + return GroovyTemplateSuperclass.modifySourceLocation2(loc); } @Override diff --git a/webserver/http-templating/src/main/java/org/webpieces/templating/impl/GroovyTemplateSuperclass.java b/webserver/http-templating/src/main/java/org/webpieces/templating/impl/GroovyTemplateSuperclass.java index 967fd576e..0dea35434 100644 --- a/webserver/http-templating/src/main/java/org/webpieces/templating/impl/GroovyTemplateSuperclass.java +++ b/webserver/http-templating/src/main/java/org/webpieces/templating/impl/GroovyTemplateSuperclass.java @@ -25,6 +25,7 @@ public abstract class GroovyTemplateSuperclass extends Script { private Map setTagProperties; private String superTemplateFilePath; private ReverseUrlLookup urlLookup; + private ThreadLocal sourceLocal = new ThreadLocal<>(); public void initialize(EscapeCharactersFormatter f, HtmlTagLookup tagLookup, Map templateProps, ReverseUrlLookup urlLookup) { @@ -47,13 +48,13 @@ public String useFormatter(Object val) { } protected void runTag(String tagName, Map args, Closure closure, String srcLocation) { - srcLocation = "\n\t"+srcLocation+"\n"; + srcLocation = modifySourceLocation2(srcLocation); HtmlTag tag = tagLookup.lookup(tagName); PrintWriter writer = (PrintWriter) getProperty(OUT_PROPERTY_NAME); try { tag.runTag(args, closure, writer, this, srcLocation); } catch(Exception e) { - throw new RuntimeException("Error running tag "+tagName+"("+e.getMessage()+"). "+srcLocation, e); + throw new RuntimeException("Error running tag #{"+tagName+"}#. See exception cause below."+srcLocation, e); } } @@ -78,7 +79,7 @@ public Map getSetTagProperties() { } public String fetchUrl(String routeId, Map args, String srcLocation) { - srcLocation = "\n\t"+srcLocation+"\n"; + srcLocation = modifySourceLocation2(srcLocation); try { Map urlParams = new HashMap<>(); for(Map.Entry entry : args.entrySet()) { @@ -98,13 +99,28 @@ public String fetchUrl(String routeId, Map args, String srcLocation) { @Override public Object getProperty(String property) { + String srcLocation = sourceLocal.get(); + srcLocation = modifySourceLocation2(srcLocation); try { return super.getProperty(property); } catch (MissingPropertyException e) { - throw new IllegalArgumentException("No such property '"+property+"' but perhaps you forgot quotes around it or you forgot to pass it in from the controller's return value(with the RouteId) OR lastly, if this is inside a custom tag, perhaps the tag did not pass in the correct arguments", e); + throw new IllegalArgumentException("No such property '"+property+"' but perhaps you forgot quotes " + + "around it or you forgot to pass it in from the controller's return value(with the RouteId) OR " + + "lastly, if this is inside a custom tag, perhaps the tag did not pass in the correct arguments."+srcLocation, e); } } + public static String modifySourceLocation2(String srcLocation) { + return "\n\n\t"+srcLocation+"\n"; + } + + public void enterExpression(String srcLocation) { + sourceLocal.set(srcLocation); + } + + public void exitExpression() { + sourceLocal.set(null); + } public ReverseUrlLookup getUrlLookup() { return urlLookup; }