When the API needs to throw an exception regarding a field, use a new FieldException...
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 17 Oct 2007 20:50:36 +0000 (16:50 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 17 Oct 2007 20:50:36 +0000 (16:50 -0400)
Api.php

diff --git a/Api.php b/Api.php
index da33e771ce0c4c4ceefd387c7d511ae247167862..7590789daf5b193bded1900ce5305f55414ed949 100644 (file)
--- a/Api.php
+++ b/Api.php
@@ -220,7 +220,7 @@ abstract class BSApi
                        {
                                if ($verify === false)
                                {
-                                       $this->_error(new Exception(sprintf(_('Validation of "%1$s" failed'), $field)));
+                                       $this->_error(new FieldException(sprintf(_('Validation of "%1$s" failed'), $field)));
                                }
                                else
                                {
@@ -421,7 +421,7 @@ abstract class BSApi
                        {
                                if (!isset($this->values["$name"]))
                                {
-                                       $this->_error(new Exception(sprintf(_('The required field "%1$s" was not set'), $name), $name));
+                                       $this->_error(new FieldException(sprintf(_('The required field "%1$s" was not set'), $name), $name));
                                }
                        }
                        else if ($options[F_REQ] == REQ_SET)
@@ -487,7 +487,7 @@ abstract class BSApi
        {
                if ($this->values["$field"] == 0)
                {
-                       return new Exception(sprintf(_('The field "%1$s" cannot be zero'), $field), $field);
+                       return new FieldException(sprintf(_('The field "%1$s" cannot be zero'), $field), $field);
                }
                
                return true;
@@ -501,7 +501,7 @@ abstract class BSApi
        {
                if (empty($this->values["$field"]))
                {
-                       return new Exception(sprintf(_('The field "%1$s" cannot be empty'), $field), $field);
+                       return new FieldException(sprintf(_('The field "%1$s" cannot be empty'), $field), $field);
                }
                
                return true;
@@ -516,7 +516,7 @@ abstract class BSApi
                $res = BSApp::Registry()->getType('Db')->queryFirst("SELECT $field FROM {$this->prefix}{$this->table} WHERE $field = " . $this->_prepareFieldForSql($field) . (empty($this->condition) ? "" : " AND !({$this->condition})"));
                if ($res)
                {
-                       return new Exception(sprintf(_('The "%1$s" field must contain a unique value'), $field), $field);
+                       return new FieldException(sprintf(_('The "%1$s" field must contain a unique value'), $field), $field);
                }
                
                return true;
@@ -574,4 +574,45 @@ class ApiException extends Exception
        }
 }
 
+/**
+ * Field Exception
+ *
+ * This exception represents a problem with an API field
+ *
+ * @author             rsesek
+ * @copyright  Copyright (c)2002 - 2007, Blue Static
+ * @version            $Id$
+ * @package            ISSO
+ *
+ */
+class FieldException extends Exception
+{
+       /**
+        * The name of the erroring field
+        * @var string
+        */
+       private $field;
+       
+       // ###################################################################
+       /**
+        * Constructor: create a new exception
+        */
+       public function __construct($error, $field)
+       {
+               $this->field = $field;
+               parent::__construct($error);
+       }
+       
+       // ###################################################################
+       /**
+        * Returns the name of the field the exception is for
+        *
+        * @return      string
+        */
+       public function getField()
+       {
+               return $this->field;
+       }
+}
+
 ?>
\ No newline at end of file