Changing the error reporting system in the API to use exceptions instead of the old...
authorRobert Sesek <rsesek@bluestatic.org>
Fri, 12 Oct 2007 19:24:08 +0000 (15:24 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Fri, 12 Oct 2007 19:24:08 +0000 (15:24 -0400)
* Api.php

Api.php

diff --git a/Api.php b/Api.php
index 44a647b17947850335390fb11aed0ee2ac897aae..4186a0a74acb22a7a0dbad68a597af6ca66b2962 100644 (file)
--- a/Api.php
+++ b/Api.php
@@ -77,12 +77,6 @@ if (!defined('REQ_AUTO'))
 */
 abstract class BSApi
 {
-       /**
-        * The callback function for the error handler
-        * @var string
-        */
-       public static $errorHandler;
-       
        /**
        * Fields: used for verification and sanitization
        * NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method), RELATION => array(FILE, CLASS IN FILE, ALTERNATE FIELD NAME))
@@ -97,7 +91,7 @@ abstract class BSApi
        public $values = array();
        
        /**
-       * Fields that were manually set with set(), not by using setExisting()
+       * Fields that were set by the client
        * @var  array
        */
        private $setfields = array();
@@ -122,9 +116,9 @@ abstract class BSApi
        
        /**
        * Error queue that builds up errors
-       * @var  array
+       * @var  ApiException
        */
-       private $errors = array();
+       private $exception = null;
        
        // ###################################################################
        /**
@@ -139,58 +133,44 @@ abstract class BSApi
        /**
        * Adds an error into the error queue that is then hit 
        *
-       * @param        string  Error message
+       * @param        Exception       Error message
        */
-       protected function _error($message)
+       protected function _error(Exception $e)
        {
-               $this->errors[] = $message;
+               if ($this->exception == null)
+               {
+                       $this->exception = new ApiException();
+               }
+               $this->exception->addException($e);
        }
        
        // ###################################################################
        /**
-       * This runs through all the errors in the error queue and processes
-       * them all at once. Error builders then get a list of all the errors,
-       * and die-by-hit callbacks stop at the first one
-       *
-       * @param        string  A string param
-       *
-       * @return       integer Return value
+       * This simply throws the ApiException if it exists, which inside holds
+       * all of the individual and specific errors
        */
        private function _processErrorQueue()
        {
-               // we want to explicitly specify silence
-               if (self::$errorHandler == 'silent')
-               {
-                       return;
-               }
-               
-               if (!is_callable(self::$errorHandler))
-               {
-                       throw new Exception('No BSApi::$errorHandler handler has been set');
-               }
-               
-               foreach ($this->errors AS $e)
+               if ($this->exception)
                {
-                       call_user_func(BSApi::$errorHandler, $e);
+                       throw $this->exception;
                }
        }
        
        // ###################################################################
        /**
-       * Returns the error list. This is because we don't want people mucking
-       * with the error system. It will return an empty array if there are
-       * no errors.
+       * Returns the list of exceptions contained in the ApiException
        *
        * @return       array   Array of errors
        */
-       public function checkErrors()
+       public function isValid()
        {
-               if (sizeof($this->errors) < 1)
+               if ($this->exception == null)
                {
                        return array();
                }
                
-               return $this->errors;
+               return $this->exception->getExceptions();
        }
        
        // ###################################################################
@@ -207,7 +187,6 @@ abstract class BSApi
                if (!isset($this->fields["$field"]))
                {
                        throw new Exception('Field "' . $field . '" is not valid');
-                       return;
                }
                
                $this->values["$field"] = ($doclean ? BSApp::GetType('Input')->clean($value, $this->fields["$field"][F_TYPE]) : $value);
@@ -229,7 +208,7 @@ abstract class BSApi
                        {
                                if ($verify === false)
                                {
-                                       $this->_error(sprintf(_('Validation of "%1$s" failed'), $field));
+                                       $this->_error(new Exception(sprintf(_('Validation of "%1$s" failed'), $field)));
                                }
                                else
                                {
@@ -257,7 +236,6 @@ abstract class BSApi
                                if (!$this->values["$field"])
                                {
                                        throw new Exception('The specified field "' . $field . '" for the condition could not be found as it is not set');
-                                       continue;
                                }
                                
                                $condbits[] = "$field = " . $this->_prepareFieldForSql($field);
@@ -277,7 +255,6 @@ abstract class BSApi
                                        if (!$this->values["$name"])
                                        {
                                                throw new Exception('Cannot determine condition from the REQ_AUTO field because it is not set');
-                                               continue;
                                        }
                                        
                                        $this->condition = "$name = " . $this->_prepareFieldForSql($name);
@@ -291,31 +268,6 @@ abstract class BSApi
                }
        }
        
-       // ###################################################################
-       /**
-       * Sets existing data into $values where it's not already present
-       */
-       public function setExisting()
-       {
-               static $run;
-               if ($run)
-               {
-                       return;
-               }
-               
-               $this->fetch();
-               
-               foreach ($this->objdata AS $field => $value)
-               {
-                       if (!isset($this->values["$field"]))
-                       {
-                               $this->values["$field"] = $value;
-                       }
-               }
-               
-               $run = true;
-       }
-       
        // ###################################################################
        /**
        * Fetches a record based on the condition
@@ -333,7 +285,7 @@ abstract class BSApi
                }
                
                // reset the error queue due to any validation errors caused by fetchable fields
-               $this->errors = array();
+               $this->errors = null;
                
                $this->_runActionMethod('pre_fetch', $doPre);
                
@@ -457,7 +409,7 @@ abstract class BSApi
                        {
                                if (!isset($this->values["$name"]))
                                {
-                                       $this->_error(sprintf(_('The required field "%1$s" was not set'), $name));
+                                       $this->_error(new Exception(sprintf(_('The required field "%1$s" was not set'), $name), $name));
                                }
                        }
                        else if ($options[F_REQ] == REQ_SET)
@@ -523,7 +475,7 @@ abstract class BSApi
        {
                if ($this->values["$field"] == 0)
                {
-                       return sprintf(_('The field "%1$s" cannot be zero'), $field);
+                       return new Exception(sprintf(_('The field "%1$s" cannot be zero'), $field), $field);
                }
                
                return true;
@@ -537,7 +489,7 @@ abstract class BSApi
        {
                if (empty($this->values["$field"]))
                {
-                       return sprintf(_('The field "%1$s" cannot be empty'), $field);
+                       return new Exception(sprintf(_('The field "%1$s" cannot be empty'), $field), $field);
                }
                
                return true;
@@ -552,11 +504,62 @@ abstract class BSApi
                $res = BSApp::GetType('Db')->queryFirst("SELECT $field FROM {$this->prefix}{$this->table} WHERE $field = " . $this->_prepareFieldForSql($field) . (empty($this->condition) ? "" : " AND !({$this->condition})"));
                if ($res)
                {
-                       return sprintf(_('The "%1$s" field must contain a unique value'), $field);
+                       return new Exception(sprintf(_('The "%1$s" field must contain a unique value'), $field), $field);
                }
                
                return true;
        }
 }
 
+/**
+ * API Exception
+ *
+ * This class is an exception container that can be used to store a series
+ * of exceptions that can be thrown as one
+ *
+ * @author             rsesek
+ * @copyright  Copyright (c)2002 - 2007, Blue Static
+ * @package            ISSO
+ *
+ */
+class ApiException extends Exception
+{
+       /**
+        * Array of exceptions
+        * @var array
+        */
+       private $exceptions = array();
+       
+       // ###################################################################
+       /**
+        * Constructor
+        */
+       public function __construct()
+       {
+               parent::__construct(_('An error occurred while processing the API data.'));
+       }
+       
+       // ###################################################################
+       /**
+        * Adds an exception to the collection
+        *
+        * @param       Exception $e
+        */
+       public function addException(Exception $e)
+       {
+               $this->exceptions[] = $e;
+       }
+       
+       // ###################################################################
+       /**
+        * Returns an array of all the exceptions in the collection
+        *
+        * @return      array
+        */
+       public function getExceptions()
+       {
+               return $this->exceptions;
+       }
+}
+
 ?>
\ No newline at end of file