You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by ni...@apache.org on 2005/03/31 17:52:10 UTC

svn commit: r159608 - struts/core/trunk/src/share/org/apache/struts/validator/validwhen/ValidWhen.java

Author: niallp
Date: Thu Mar 31 07:52:09 2005
New Revision: 159608

URL: http://svn.apache.org/viewcvs?view=rev&rev=159608
Log:
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/trunk/src/share/org/apache/struts/validator/validwhen/ValidWhen.java

Modified: struts/core/trunk/src/share/org/apache/struts/validator/validwhen/ValidWhen.java
URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/validator/validwhen/ValidWhen.java?view=diff&r1=159607&r2=159608
==============================================================================
--- struts/core/trunk/src/share/org/apache/struts/validator/validwhen/ValidWhen.java (original)
+++ struts/core/trunk/src/share/org/apache/struts/validator/validwhen/ValidWhen.java Thu Mar 31 07:52:09 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: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org