SLING-7710 - [HTL] Enhance error reporting for plugin errors
authorRadu Cotescu <radu@apache.org>
Wed, 6 Jun 2018 13:52:48 +0000 (15:52 +0200)
committerRadu Cotescu <radu@apache.org>
Wed, 6 Jun 2018 13:52:48 +0000 (15:52 +0200)
src/main/java/org/apache/sling/scripting/sightly/impl/html/dom/MarkupHandler.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/CallPlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/TemplatePlugin.java

index 58c6ccb..0e41724 100644 (file)
@@ -32,6 +32,7 @@ import org.apache.sling.scripting.sightly.compiler.commands.OutputVariable;
 import org.apache.sling.scripting.sightly.compiler.commands.VariableBinding;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
+import org.apache.sling.scripting.sightly.compiler.expression.MarkupContext;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperator;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.BooleanConstant;
@@ -51,7 +52,6 @@ import org.apache.sling.scripting.sightly.impl.compiler.util.SymbolGenerator;
 import org.apache.sling.scripting.sightly.impl.filter.ExpressionContext;
 import org.apache.sling.scripting.sightly.impl.filter.Filter;
 import org.apache.sling.scripting.sightly.impl.html.MarkupUtils;
-import org.apache.sling.scripting.sightly.compiler.expression.MarkupContext;
 import org.apache.sling.scripting.sightly.impl.plugin.Plugin;
 import org.apache.sling.scripting.sightly.impl.plugin.PluginCallInfo;
 import org.apache.sling.scripting.sightly.impl.plugin.PluginInvoke;
@@ -85,7 +85,15 @@ public class MarkupHandler {
     public void onAttribute(String name, String value, char quoteChar) {
         ElementContext context = elementStack.peek();
         if (Syntax.isPluginAttribute(name)) {
-            handlePlugin(name, StringUtils.defaultString(value, ""), context);
+            try {
+                handlePlugin(name, StringUtils.defaultString(value, ""), context);
+            } catch (SightlyCompilerException e) {
+                if (StringUtils.isEmpty(e.getOffendingInput())) {
+                    throw new SightlyCompilerException(e.getMessage(),
+                            name + (StringUtils.isNotEmpty(value) ? "=" + quoteChar + value + quoteChar : ""));
+                }
+                throw e;
+            }
         } else {
             context.addAttribute(name, value, quoteChar);
         }
index 455612e..b666575 100644 (file)
@@ -39,7 +39,7 @@ public class CallPlugin extends AbstractPlugin {
                                final PluginCallInfo callInfo,
                                final CompilerContext compilerContext) {
         if (callInfo.getArguments().length > 0) {
-            throw new SightlyCompilerException("Call plugin should have no arguments.", "data-sly-call." + callInfo.getArguments()[0]);
+            throw new SightlyCompilerException("Call plugin should have no arguments.");
         }
         return new DefaultPluginInvoke() {
 
index 2890c19..a22116f 100644 (file)
@@ -41,6 +41,8 @@ public class TemplatePlugin extends AbstractPlugin {
 
     @Override
     public PluginInvoke invoke(final Expression expressionNode, final PluginCallInfo callInfo, CompilerContext compilerContext) {
+        final String templateName = decodeName(callInfo);
+
         return new DefaultPluginInvoke() {
 
             @Override
@@ -51,9 +53,8 @@ public class TemplatePlugin extends AbstractPlugin {
 
             @Override
             public void beforeElement(PushStream stream, String tagName) {
-                String name = decodeName();
                 Set<String> parameters = extractParameters();
-                stream.write(new Procedure.Start(name, parameters));
+                stream.write(new Procedure.Start(templateName, parameters));
             }
 
             @Override
@@ -81,13 +82,14 @@ public class TemplatePlugin extends AbstractPlugin {
                 return options.keySet();
             }
 
-            private String decodeName() {
-                String[] arguments = callInfo.getArguments();
-                if (arguments.length == 0) {
-                    throw new SightlyCompilerException("Template name was not provided.", "data-sly-template=");
-                }
-                return arguments[0];
-            }
         };
     }
+
+    private String decodeName(PluginCallInfo callInfo) {
+        String[] arguments = callInfo.getArguments();
+        if (arguments.length == 0) {
+            throw new SightlyCompilerException("Template name was not provided.");
+        }
+        return arguments[0];
+    }
 }