Author: niallp
Date: Thu Mar 31 08:10:16 2005
New Revision: 159609

URL: http://svn.apache.org/viewcvs?view=rev&rev=159609
Log:
Port Improved ValidWhen Exception Handling to 1.2.X Branch

Improve ValidWhen Exception Handling - exceptions are now logged and validation 
fails returning an error message with details of the error.

The problem with ValidWhen is that currently if there is an exception 
processing the 'test' expression from the validation.xml  then it just prints a 
stack trace and returns 'false'. However returning 'false' doesn't cause the 
validation to fail - for validation to fail and error message also needs to be 
added. This causes alot of confusion for users, since if they mess up the 
'test' expression, then validation passes! This change rectifys that.

Modified:
    
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/validator/validwhen/ValidWhen.java

Modified: 
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/validator/validwhen/ValidWhen.java
URL: 
http://svn.apache.org/viewcvs/struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/validator/validwhen/ValidWhen.java?view=diff&r1=159608&r2=159609
==============================================================================
--- 
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/validator/validwhen/ValidWhen.java
 (original)
+++ 
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/validator/validwhen/ValidWhen.java
 Thu Mar 31 08:10:16 2005
@@ -27,7 +27,10 @@
 import org.apache.commons.validator.ValidatorAction;
 import org.apache.commons.validator.util.ValidatorUtils;
 import org.apache.struts.action.ActionMessages;
+import org.apache.struts.action.ActionMessage;
 import org.apache.struts.validator.Resources;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * This class contains the validwhen validation that is used in the 
@@ -38,6 +41,11 @@
 public class ValidWhen {
 
     /**
+     *  Commons Logging instance.
+     */
+    private static final Log log = LogFactory.getLog(ValidWhen.class);
+
+    /**
      * Returns true if <code>obj</code> is null or a String.
      */
     private static boolean isString(Object obj) {
@@ -97,12 +105,36 @@
         
         String test = field.getVarValue("test");
         if (test == null) {
+            String msg = "ValidWhen Error 'test' parameter is missing for 
field ' " + field.getKey() + "'";
+            errors.add(field.getKey(), new ActionMessage(msg, false));
+            log.error(msg);
             return false;
         }
         
-        ValidWhenLexer lexer = new ValidWhenLexer(new StringReader(test));
+        // Create the Lexer
+        ValidWhenLexer lexer= null;
+        try {
+            lexer = new ValidWhenLexer(new StringReader(test));
+        } catch (Exception ex) {
+            String msg = "ValidWhenLexer Error for field ' " + field.getKey() 
+ "' - " + ex;
+            errors.add(field.getKey(), new ActionMessage(msg + " - " + ex, 
false));
+            log.error(msg);
+            log.debug(msg, ex);
+            return false;
+        }
+
+        // Create the Parser
+        ValidWhenParser parser = null;
+        try {
+            parser = new ValidWhenParser(lexer);
+        } catch (Exception ex) {
+            String msg = "ValidWhenParser Error for field ' " + field.getKey() 
+ "' - " + ex;
+            errors.add(field.getKey(), new ActionMessage(msg, false));
+            log.error(msg);
+            log.debug(msg, ex);
+            return false;
+        }
 
-        ValidWhenParser parser = new ValidWhenParser(lexer);
 
         parser.setForm(form);
         parser.setIndex(index);
@@ -113,11 +145,15 @@
             valid = parser.getResult();
             
         } catch (Exception ex) {
-            ex.printStackTrace();
-            
-            errors.add(
-                field.getKey(),
-                Resources.getActionMessage(validator, request, va, field));
+
+            // errors.add(
+            //    field.getKey(),
+            //    Resources.getActionMessage(validator, request, va, field));
+
+            String msg = "ValidWhen Error for field ' " + field.getKey() + "' 
- " + ex;
+            errors.add(field.getKey(), new ActionMessage(msg, false));
+            log.error(msg);
+            log.debug(msg, ex);
                 
             return false;
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to