You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2016/07/18 14:43:23 UTC

svn commit: r1753233 - in /velocity/engine/trunk: src/changes/ velocity-engine-core/src/main/java/org/apache/velocity/app/event/ velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/ velocity-engine-core/src/main/java/org/apache/v...

Author: cbrisson
Date: Mon Jul 18 14:43:23 2016
New Revision: 1753233

URL: http://svn.apache.org/viewvc?rev=1753233&view=rev
Log:
[engine] have MethodExceptionEventHandler provide template location infos

Modified:
    velocity/engine/trunk/src/changes/changes.xml
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java
    velocity/engine/trunk/velocity-engine-examples/src/main/java/org/apache/velocity/example/EventExample.java

Modified: velocity/engine/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/changes/changes.xml?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/src/changes/changes.xml (original)
+++ velocity/engine/trunk/src/changes/changes.xml Mon Jul 18 14:43:23 2016
@@ -27,6 +27,10 @@
   <body>
     <release version="2.0" date="In Subversion">
 
+      <action type="add" dev="cbrisson" issue="VELOCITY-746">
+        MethodExceptionEventHandler now provide template location infos
+      </action>
+      
       <action type="fix" dev="cbrisson" issue="VELOCITY-797" due-to="Jarkko Viinamäki">
         Attach macros to their defining template. Also fixes VELOCITY-776. Thanks to Simon Kitching for the multithreading testcase.
       </action>

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventHandlerUtil.java Mon Jul 18 14:43:23 2016
@@ -129,7 +129,7 @@ public class EventHandlerUtil {
      */
     public static Object methodException(RuntimeServices rsvc,
             InternalContextAdapter context, Class claz, String method,
-            Exception e) throws Exception 
+            Exception e, Info info) throws Exception
     {
         // app level cartridges have already been initialized
         EventCartridge ev1 = rsvc.getApplicationEventCartridge();
@@ -142,7 +142,7 @@ public class EventHandlerUtil {
             (ev2 == null) ? null: ev2.getMethodExceptionEventHandlers();              
         
         EventHandlerMethodExecutor methodExecutor = 
-            new MethodExceptionEventHandler.MethodExceptionExecutor(context, claz, method, e);
+            new MethodExceptionEventHandler.MethodExceptionExecutor(context, claz, method, e, info);
         
         if ( ((applicationEventHandlerIterator == null) || !applicationEventHandlerIterator.hasNext()) &&
                 ((contextEventHandlerIterator == null) || !contextEventHandlerIterator.hasNext()) )

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/MethodExceptionEventHandler.java Mon Jul 18 14:43:23 2016
@@ -2,6 +2,7 @@ package org.apache.velocity.app.event;
 
 import org.apache.velocity.context.Context;
 import org.apache.velocity.util.ContextAware;
+import org.apache.velocity.util.introspection.Info;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -43,10 +44,11 @@ public interface MethodExceptionEventHan
      * @param claz the class of the object the method is being applied to
      * @param method the method
      * @param e the thrown exception
+     * @param info contains template, line, column details
      * @return an object to insert in the page
      * @throws RuntimeException an exception to be thrown instead inserting an object
      */
-    public Object methodException( Class claz, String method, Exception e );
+    public Object methodException( Class claz, String method, Exception e, Info info );
 
     /**
      * Defines the execution strategy for methodException
@@ -58,6 +60,7 @@ public interface MethodExceptionEventHan
         private Class claz;
         private String method;
         private Exception e;
+        private Info info;
         
         private Object result;
         private boolean executed = false;
@@ -66,12 +69,14 @@ public interface MethodExceptionEventHan
                 Context context, 
                 Class claz,
                 String method,
-                Exception e)
+                Exception e,
+                Info info)
         {
             this.context = context;
             this.claz = claz;
             this.method = method;
             this.e = e;
+            this.info = info;
         }
 
         /**
@@ -88,7 +93,7 @@ public interface MethodExceptionEventHan
                 ((ContextAware) eh).setContext(context);
 
             executed = true;
-            result = ((MethodExceptionEventHandler) handler).methodException(claz, method, e);
+            result = ((MethodExceptionEventHandler) handler).methodException(claz, method, e, info);
         }
 
         public Object getReturnValue()

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/PrintExceptions.java Mon Jul 18 14:43:23 2016
@@ -22,6 +22,7 @@ package org.apache.velocity.app.event.im
 import org.apache.velocity.app.event.MethodExceptionEventHandler;
 import org.apache.velocity.runtime.RuntimeServices;
 import org.apache.velocity.util.RuntimeServicesAware;
+import org.apache.velocity.util.introspection.Info;
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
@@ -30,20 +31,25 @@ import java.io.StringWriter;
  * Simple event handler that renders method exceptions in the page
  * rather than throwing the exception.  Useful for debugging.
  *
- * <P>By default this event handler renders the exception name only.
- * To include both the exception name and the message, set the property
- * <code>eventhandler.methodexception.message</code> to <code>true</code>.  To render
- * the stack trace, set the property <code>eventhandler.methodexception.stacktrace</code>
+ * <P>By default this event handler renders an error message containing the class and method which generated
+ * the exception, the exception name and its message.
+ *
+ * To render the reference and the location in the tempkate, set the property <code>eventhandler.methodexception.templateinfo</code>
+ * to <code>true</code>.
+ *
+ * To render the stack trace, set the property <code>eventhandler.methodexception.stacktrace</code>
  * to <code>true</code>.
  *
+ *
  * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
+ * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
  * @version $Id$
  * @since 1.5
  */
 public class PrintExceptions implements MethodExceptionEventHandler, RuntimeServicesAware
 {
 
-    private static String SHOW_MESSAGE = "eventhandler.methodexception.message";
+    private static String SHOW_TEMPLATE_INFO = "eventhandler.methodexception.templateinfo";
     private static String SHOW_STACK_TRACE = "eventhandler.methodexception.stacktrace";
 
     /** Reference to the runtime service */
@@ -55,44 +61,30 @@ public class PrintExceptions implements
      * @param claz the class of the object the method is being applied to
      * @param method the method
      * @param e the thrown exception
+     * @param info template name and line, column informations
      * @return an object to insert in the page
      */
-    public Object methodException(Class claz, String method, Exception e)
+    public Object methodException(Class claz, String method, Exception e, Info info)
     {
-        boolean showMessage = rs.getBoolean(SHOW_MESSAGE,false);
+        boolean showTemplateInfo = rs.getBoolean(SHOW_TEMPLATE_INFO, false);
         boolean showStackTrace = rs.getBoolean(SHOW_STACK_TRACE,false);
 
-        StringBuffer st;
-        if (showMessage && showStackTrace)
-        {
-            st = new StringBuffer(200);
-            st.append(e.getClass().getName()).append("\n");
-            st.append(e.getMessage()).append("\n");
-            st.append(getStackTrace(e));
+        StringBuffer st = new StringBuffer();
+        st.append("Exception while executing method ").append(claz.toString()).append(".").append(method);
+        st.append(": ").append(e.getClass().getName()).append(": ").append(e.getMessage());
 
-        } else if (showMessage)
+        if (showTemplateInfo)
         {
-            st = new StringBuffer(50);
-            st.append(e.getClass().getName()).append("\n");
-            st.append(e.getMessage()).append("\n");
-
-        } else if (showStackTrace)
-        {
-            st = new StringBuffer(200);
-            st.append(e.getClass().getName()).append("\n");
-            st.append(getStackTrace(e));
-
-        } else
+            st.append(" at ").append(info.getTemplateName()).append(" (line ").append(info.getLine()).append(", column ").append(info.getColumn()).append(")");
+        }
+        if (showStackTrace)
         {
-            st = new StringBuffer(15);
-            st.append(e.getClass().getName()).append("\n");
+            st.append("\n").append(getStackTrace(e));
         }
-
         return st.toString();
 
     }
 
-
     private static String getStackTrace(Throwable throwable)
     {
         PrintWriter printWriter = null;

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java Mon Jul 18 14:43:23 2016
@@ -210,7 +210,7 @@ public class ASTIdentifier extends Simpl
                 try
                 {
                     return EventHandlerUtil.methodException(rsvc, context, o.getClass(), vg.getMethodName(),
-                            (Exception) t);
+                            (Exception) t, uberInfo);
                 }
 
                 /**

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTMethod.java Mon Jul 18 14:43:23 2016
@@ -253,7 +253,7 @@ public class ASTMethod extends SimpleNod
         {
             try
             {
-                return EventHandlerUtil.methodException( rsvc, context, o.getClass(), methodName, (Exception) t );
+                return EventHandlerUtil.methodException( rsvc, context, o.getClass(), methodName, (Exception) t, uberInfo );
             }
 
             /**

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/BuiltInEventHandlerTestCase.java Mon Jul 18 14:43:23 2016
@@ -320,7 +320,7 @@ public class BuiltInEventHandlerTestCase
 
         VelocityEngine ve2 = new VelocityEngine();
         ve2.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, "org.apache.velocity.app.event.implement.PrintExceptions");
-        ve2.setProperty("eventhandler.methodexception.message","true");
+        ve2.setProperty("eventhandler.methodexception.templateinfo","true");
         ve2.init();
 
         VelocityEngine ve3 = new VelocityEngine();
@@ -334,25 +334,32 @@ public class BuiltInEventHandlerTestCase
         context = new VelocityContext();
         context.put("list",new ArrayList());
 
-        // exception only
+        // exception and message only
         writer = new StringWriter();
-        ve1.evaluate(context,writer,"test","$list.get(0)");
-        assertTrue(writer.toString().indexOf("IndexOutOfBoundsException") != -1);
-        assertTrue(writer.toString().indexOf("Index: 0, Size: 0") == -1);
-        assertTrue(writer.toString().indexOf("ArrayList") == -1);
+        ve1.evaluate(context, writer, "test", "$list.get(0)");
+        String result = writer.toString();
+        assertTrue(result.indexOf("IndexOutOfBoundsException") != -1);
+        assertTrue(result.indexOf("Index: 0, Size: 0") != -1);
+        assertTrue(result.indexOf("at test (line 1, column 7)") == -1);
+        assertTrue(result.indexOf("rangeCheck") == -1);
 
-        // message
+        // exception, message and template info
         writer = new StringWriter();
         ve2.evaluate(context,writer,"test","$list.get(0)");
-        assertTrue(writer.toString().indexOf("IndexOutOfBoundsException") != -1);
-        assertTrue(writer.toString().indexOf("Index: 0, Size: 0") != -1);
-        assertTrue(writer.toString().indexOf("ArrayList") == -1);
+        result = writer.toString();;
+        assertTrue(result.indexOf("IndexOutOfBoundsException") != -1);
+        assertTrue(result.indexOf("Index: 0, Size: 0") != -1);
+        assertTrue(result.indexOf("at test (line 1, column 7)") != -1);
+        assertTrue(result.indexOf("rangeCheck") == -1);
 
-        // stack trace
+        // exception, message and stack trace
         writer = new StringWriter();
         ve3.evaluate(context,writer,"test","$list.get(0)");
-        assertTrue(writer.toString().indexOf("IndexOutOfBoundsException") != -1);
-        assertTrue(writer.toString().indexOf("ArrayList") != -1);
+        result = writer.toString();;
+        assertTrue(result.indexOf("IndexOutOfBoundsException") != -1);
+        assertTrue(result.indexOf("Index: 0, Size: 0") != -1);
+        assertTrue(result.indexOf("at test (line 1, column 7)") == -1);
+        assertTrue(result.indexOf("rangeCheck") != -1);
 
         log("PrintException handler successful.");
 

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/EventHandlingTestCase.java Mon Jul 18 14:43:23 2016
@@ -29,6 +29,7 @@ import org.apache.velocity.runtime.Runti
 import org.apache.velocity.runtime.RuntimeServices;
 import org.apache.velocity.util.ContextAware;
 import org.apache.velocity.util.RuntimeServicesAware;
+import org.apache.velocity.util.introspection.Info;
 
 /**
  * Tests event handling for all event handlers except IncludeEventHandler.  This is tested
@@ -232,7 +233,7 @@ public class EventHandlingTestCase exten
         /**
          *  Handles exceptions thrown during in-template method access
          */
-        public Object methodException( Class claz, String method, Exception e )
+        public Object methodException( Class claz, String method, Exception e, Info info )
         {
             // as a test, make sure this EventHandler is initialized
             if (rs == null)

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler1.java Mon Jul 18 14:43:23 2016
@@ -22,6 +22,7 @@ package org.apache.velocity.test.eventha
 import org.apache.velocity.app.event.IncludeEventHandler;
 import org.apache.velocity.app.event.MethodExceptionEventHandler;
 import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+import org.apache.velocity.util.introspection.Info;
 
 /**
  * This is a test set of event handlers, used to test event handler sequences.
@@ -46,7 +47,7 @@ public class Handler1
         /**
          * throw the exception
          */
-        public Object methodException(Class claz, String method, Exception e)
+        public Object methodException(Class claz, String method, Exception e, Info info)
         {
             throw new RuntimeException(e);
         }

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/eventhandler/Handler2.java Mon Jul 18 14:43:23 2016
@@ -22,6 +22,7 @@ package org.apache.velocity.test.eventha
 import org.apache.velocity.app.event.IncludeEventHandler;
 import org.apache.velocity.app.event.MethodExceptionEventHandler;
 import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+import org.apache.velocity.util.introspection.Info;
 
 /**
  * This is a test set of event handlers, used to test event handler sequences.
@@ -46,7 +47,7 @@ public class Handler2
     /**
      * print the exception
      */
-    public Object methodException(Class claz, String method, Exception e)
+    public Object methodException(Class claz, String method, Exception e, Info info)
     {
         return "Exception: " + e;
     }

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/ExceptionGeneratingEventHandler.java Mon Jul 18 14:43:23 2016
@@ -22,6 +22,7 @@ package org.apache.velocity.test.misc;
 import org.apache.velocity.app.event.IncludeEventHandler;
 import org.apache.velocity.app.event.MethodExceptionEventHandler;
 import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
+import org.apache.velocity.util.introspection.Info;
 
 /**
  * Event handlers that always throws an exception.  Used to test
@@ -40,10 +41,7 @@ public class ExceptionGeneratingEventHan
         throw new RuntimeException("exception");
     }
 
-    public Object methodException(Class claz, String method, Exception e)
-    {
-        throw new RuntimeException("exception");
-    }
+    public Object methodException(Class claz, String method, Exception e, Info info) { throw new RuntimeException("exception"); }
 
     public Object referenceInsert(String reference, Object value)
     {

Modified: velocity/engine/trunk/velocity-engine-examples/src/main/java/org/apache/velocity/example/EventExample.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-examples/src/main/java/org/apache/velocity/example/EventExample.java?rev=1753233&r1=1753232&r2=1753233&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-examples/src/main/java/org/apache/velocity/example/EventExample.java (original)
+++ velocity/engine/trunk/velocity-engine-examples/src/main/java/org/apache/velocity/example/EventExample.java Mon Jul 18 14:43:23 2016
@@ -19,6 +19,8 @@ package org.apache.velocity.example;
  */
 
 import java.io.StringWriter;
+
+import org.apache.velocity.util.introspection.Info;
 import org.slf4j.helpers.FormattingTuple;
 import org.slf4j.helpers.MarkerIgnoringBase;
 import org.slf4j.helpers.MessageFormatter;
@@ -279,7 +281,7 @@ public class EventExample extends Marker
         return true;
     }
 
-    public Object methodException( Class claz, String method, Exception e )   {
+    public Object methodException( Class claz, String method, Exception e, Info info )   {
         /*
          *  only do processing if the switch is on
          */