Author: nbubna
Date: Mon Oct 13 10:29:25 2008
New Revision: 704172

URL: http://svn.apache.org/viewvc?rev=704172&view=rev
Log:
VELOCITY-626 log full macro stack when a runtime exception occurs (thx to Byron 
Foster)

Modified:
    
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
    
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
    
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java

Modified: 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=704172&r1=704171&r2=704172&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
 (original)
+++ 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
 Mon Oct 13 10:29:25 2008
@@ -237,12 +237,34 @@
             }
             catch (TemplateInitException die)
             {
-              Info info = new Info(sourceTemplate, node.getLine(), 
node.getColumn());
-
+                Info info = new Info(sourceTemplate, node.getLine(), 
node.getColumn());
                 throw new ParseErrorException(die.getMessage() + " at "
                     + Log.formatFileString(info), info);
             }
-            return vmProxy.render(context, writer, node);
+
+            try
+            {
+                return vmProxy.render(context, writer, node);
+            }
+            catch (RuntimeException e)
+            {
+                /**
+                 * We catch, the exception here so that we can record in
+                 * the logs the template and line number of the macro call
+                 * which generate the exception.  This information is
+                 * especially important for multiple macro call levels.
+                 * this is also true for the following catch blocks.
+                 */
+                rsvc.getLog().error("Exception in macro #" + macroName + " at 
" +
+                  Log.formatFileString(sourceTemplate, getLine(), 
getColumn()));
+                throw e;
+            }
+            catch (IOException e)
+            {
+                rsvc.getLog().error("Exception in macro #" + macroName + " at 
" +
+                  Log.formatFileString(sourceTemplate, getLine(), 
getColumn()));
+                throw e;
+            }
         }
         else if (strictRef)
         {

Modified: 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java?rev=704172&r1=704171&r2=704172&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
 (original)
+++ 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/VelocimacroProxy.java
 Mon Oct 13 10:29:25 2008
@@ -210,10 +210,6 @@
             vmc.popCurrentMacroName();
             return true;
         }
-        catch (MethodInvocationException e)
-        {
-            throw e;
-        }
         catch (RuntimeException e)
         {
             throw e;

Modified: 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=704172&r1=704171&r2=704172&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
 (original)
+++ 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
 Mon Oct 13 10:29:25 2008
@@ -542,12 +542,8 @@
 
         if (result == null)
         {
-            String msg = "reference set : template = "
-                + context.getCurrentTemplateName() +
-                " [line " + getLine() + ",column " +
-                getColumn() + "] : " + literal() +
-                " is not a valid reference.";
-            
+            String msg = "reference set is not a valid reference at "
+                    + Log.formatFileString(uberInfo);
             log.error(msg);
             return false;
         }
@@ -570,12 +566,8 @@
                         jjtGetChild(i+1).getLine(), 
jjtGetChild(i+1).getColumn());
                 }            
               
-                String msg = "reference set : template = "
-                    + context.getCurrentTemplateName() +
-                    " [line " + getLine() + ",column " +
-                    getColumn() + "] : " + literal() +
-                    " is not a valid reference.";
-                
+                String msg = "reference set is not a valid reference at "
+                    + Log.formatFileString(uberInfo);
                 log.error(msg);
 
                 return false;
@@ -850,14 +842,27 @@
      */
     public Object getVariableValue(Context context, String variable) throws 
MethodInvocationException
     {
-        Object obj = context.get(variable);
+        Object obj = null;
+        try
+        {
+            obj = context.get(variable);
+        }
+        catch(RuntimeException e)
+        {
+            log.error("Exception calling reference $" + variable + " at "
+                      + Log.formatFileString(uberInfo));
+            throw e;
+        }
+        
         if (strictRef && obj == null)
         {
           if (!context.containsKey(variable))
           {
-            throw new MethodInvocationException("Variable '" + variable +
-                "' has not been set", null, identifier,
-                uberInfo.getTemplateName(), uberInfo.getLine(), 
uberInfo.getColumn());            
+              log.error("Variable $" + variable + " has not been set at "
+                        + Log.formatFileString(uberInfo));
+              throw new MethodInvocationException("Variable $" + variable +
+                  " has not been set", null, identifier,
+                  uberInfo.getTemplateName(), uberInfo.getLine(), 
uberInfo.getColumn());            
           }
         }
         return obj;        


Reply via email to