You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2013/05/23 03:16:39 UTC

[2/2] git commit: Improve exception reporting for invalid source files

Improve exception reporting for invalid source files


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/74dcf0bd
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/74dcf0bd
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/74dcf0bd

Branch: refs/heads/master
Commit: 74dcf0bd0daf18a3848c097d3e88e8c69d0aef5d
Parents: 09a94cd
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Wed May 22 17:57:59 2013 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed May 22 17:57:59 2013 -0700

----------------------------------------------------------------------
 .../wro4j/ResourceTransformerFactoryImpl.java      |   35 +++++++++----
 .../tapestry5/wro4j/modules/WRO4JModule.java       |   41 +++++++++++++++
 2 files changed, 65 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/74dcf0bd/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
index 5893bd1..cd5005d 100644
--- a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
+++ b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
@@ -14,6 +14,8 @@
 
 package org.apache.tapestry5.internal.wro4j;
 
+import org.apache.tapestry5.ioc.IOOperation;
+import org.apache.tapestry5.ioc.OperationTracker;
 import org.apache.tapestry5.ioc.Resource;
 import org.apache.tapestry5.services.assets.ResourceDependencies;
 import org.apache.tapestry5.services.assets.ResourceTransformer;
@@ -32,10 +34,13 @@ public class ResourceTransformerFactoryImpl implements ResourceTransformerFactor
 
     private final ResourceProcessorSource source;
 
-    public ResourceTransformerFactoryImpl(Logger logger, ResourceProcessorSource source)
+    private final OperationTracker tracker;
+
+    public ResourceTransformerFactoryImpl(Logger logger, ResourceProcessorSource source, OperationTracker tracker)
     {
         this.logger = logger;
         this.source = source;
+        this.tracker = tracker;
     }
 
     public ResourceTransformer createCompiler(final String contentType, String processorName, final String sourceName, final String targetName)
@@ -49,21 +54,29 @@ public class ResourceTransformerFactoryImpl implements ResourceTransformerFactor
                 return contentType;
             }
 
-            public InputStream transform(Resource source, ResourceDependencies dependencies) throws IOException
+            public InputStream transform(final Resource source, ResourceDependencies dependencies) throws IOException
             {
-                final long startTime = System.nanoTime();
+                final String description = String.format("Compiling %s from %s to %s", source, sourceName, targetName);
+
+                return tracker.perform(description, new IOOperation<InputStream>()
+                {
+                    public InputStream perform() throws IOException
+                    {
+                        final long startTime = System.nanoTime();
 
-                InputStream result = compiler.process(String.format("Compiling %s from %s to %s", source, sourceName, targetName),
-                        source.toURL().toString(),
-                        source.openStream(), contentType);
+                        InputStream result = compiler.process(description,
+                                source.toURL().toString(),
+                                source.openStream(), contentType);
 
-                final long elapsedTime = System.nanoTime() - startTime;
+                        final long elapsedTime = System.nanoTime() - startTime;
 
-                logger.info(String.format("Compiled %s to %s in %.2f ms",
-                        source, targetName,
-                        ((double) elapsedTime) * NANOS_TO_MILLIS));
+                        logger.info(String.format("Compiled %s to %s in %.2f ms",
+                                source, targetName,
+                                ((double) elapsedTime) * NANOS_TO_MILLIS));
 
-                return result;
+                        return result;
+                    }
+                });
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/74dcf0bd/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java b/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
index 7874d73..499ae88 100644
--- a/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
+++ b/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
@@ -14,6 +14,8 @@
 
 package org.apache.tapestry5.wro4j.modules;
 
+import com.github.sommeri.less4j.core.parser.AntlrException;
+import org.apache.tapestry5.MarkupWriter;
 import org.apache.tapestry5.internal.wro4j.*;
 import org.apache.tapestry5.ioc.MappedConfiguration;
 import org.apache.tapestry5.ioc.ObjectCreator;
@@ -21,6 +23,9 @@ import org.apache.tapestry5.ioc.ObjectLocator;
 import org.apache.tapestry5.ioc.ServiceBinder;
 import org.apache.tapestry5.ioc.annotations.Contribute;
 import org.apache.tapestry5.ioc.annotations.Primary;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
+import org.apache.tapestry5.services.ObjectRenderer;
 import org.apache.tapestry5.services.assets.ResourceMinimizer;
 import org.apache.tapestry5.services.assets.ResourceTransformer;
 import org.apache.tapestry5.services.assets.StreamableResourceSource;
@@ -31,6 +36,8 @@ import ro.isdc.wro.extensions.processor.js.RhinoCoffeeScriptProcessor;
 import ro.isdc.wro.extensions.processor.support.coffeescript.CoffeeScript;
 import ro.isdc.wro.model.resource.processor.impl.css.CssCompressorProcessor;
 
+import java.util.List;
+
 /**
  * Configures CoffeeScript-to-JavaScript compilation.
  *
@@ -121,4 +128,38 @@ public class WRO4JModule
         configuration.addInstance("text/css", CSSMinimizer.class);
         configuration.addInstance("text/javascript", JavaScriptMinimizer.class);
     }
+
+    @Contribute(ObjectRenderer.class)
+    @Primary
+    public static void decodeLessErrors(MappedConfiguration<Class, ObjectRenderer> configuration)
+    {
+        configuration.add(AntlrException.class, new ObjectRenderer<AntlrException>()
+        {
+            public void render(AntlrException e, MarkupWriter writer)
+            {
+                List<String> strings = CollectionFactory.newList();
+
+                if (InternalUtils.isNonBlank(e.getMessage()))
+                {
+                    strings.add(e.getMessage());
+                }
+
+                // Inside WRO4J we see that the LessSource is a StringSource with no useful toString(), so
+                // it is omitted. We may need to create our own processors, stripping away a couple of layers of
+                // WRO4J to get proper exception reporting!
+
+                if (e.getLine() > 0)
+                {
+                    strings.add("line " + e.getLine());
+                }
+
+                if (e.getCharacter() > 0)
+                {
+                    strings.add("position " + e.getCharacter());
+                }
+
+                writer.write(InternalUtils.join(strings, " - "));
+            }
+        });
+    }
 }