Skip to content

Commit

Permalink
add information when a property is missing with file and line number
Browse files Browse the repository at this point in the history
  • Loading branch information
deanhiller committed Aug 11, 2016
1 parent db3eaa4 commit 2cdbd22
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -51,7 +52,7 @@ public String getSourceLocation(boolean dueToError) {
if(!dueToError)
return loc;

return "\n\t"+loc+"\n";
return GroovyTemplateSuperclass.modifySourceLocation2(loc);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class GroovyTemplateSuperclass extends Script {
private Map<Object, Object> setTagProperties;
private String superTemplateFilePath;
private ReverseUrlLookup urlLookup;
private ThreadLocal<String> sourceLocal = new ThreadLocal<>();

public void initialize(EscapeCharactersFormatter f, HtmlTagLookup tagLookup,
Map<Object, Object> templateProps, ReverseUrlLookup urlLookup) {
Expand All @@ -47,13 +48,13 @@ public String useFormatter(Object val) {
}

protected void runTag(String tagName, Map<Object, Object> 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);
}
}

Expand All @@ -78,7 +79,7 @@ public Map<Object, Object> getSetTagProperties() {
}

public String fetchUrl(String routeId, Map<?, ?> args, String srcLocation) {
srcLocation = "\n\t"+srcLocation+"\n";
srcLocation = modifySourceLocation2(srcLocation);
try {
Map<String, String> urlParams = new HashMap<>();
for(Map.Entry<?, ?> entry : args.entrySet()) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 2cdbd22

Please sign in to comment.