]> granicus.if.org Git - php/commitdiff
@Added raiseError and setErrorHandling methods to PEAR class (Stig, PEAR)
authorStig Bakken <ssb@php.net>
Tue, 30 Jan 2001 00:55:27 +0000 (00:55 +0000)
committerStig Bakken <ssb@php.net>
Tue, 30 Jan 2001 00:55:27 +0000 (00:55 +0000)
# This allows all objects to have their own default error handling
# or use a global default.

pear/PEAR.php.in
pear/tests/pear_error.phpt
pear/tests/pear_error3.phpt [new file with mode: 0644]

index 88d3ca474cab0c65fff93b8b2b2a40aca1aa674c..4f7fe166c9c81b6778d562afb3df38a7df57f323 100644 (file)
@@ -30,6 +30,9 @@ define('PHP_BINDIR', '@prefix@/bin');
 define('PEAR_INSTALL_DIR', '@PEAR_INSTALLDIR@');
 define('PEAR_EXTENSION_DIR', '@EXTENSION_DIR@');
 
+$_PEAR_default_error_mode = PEAR_ERROR_RETURN;
+$_PEAR_default_error_options = E_USER_NOTICE;
+$_PEAR_default_error_callback = '';
 $_PEAR_destructor_object_list = array();
 
 //
@@ -60,6 +63,9 @@ class PEAR
        // {{{ properties
 
        var $_debug = false;
+    var $_default_error_mode = null;
+    var $_default_error_options = null;
+    var $_default_error_handler = '';
 
        // }}}
 
@@ -114,6 +120,124 @@ class PEAR
                                           is_subclass_of($data, "pear_error")));
        }
 
+    // }}}
+    // {{{ setErrorHandling()
+
+    /**
+     * Sets how errors generated by this DB object should be handled.
+     * Can be invoked both in objects and statically.  If called
+     * statically, setErrorHandling sets the default behaviour for all
+     * PEAR objects.  If called in an object, setErrorHandling sets
+     * the default behaviour for that object.
+     *
+     * @param $mode int
+     *        one of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
+     *        PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or
+     *        PEAR_ERROR_CALLBACK.
+     *
+     * @param $options mixed
+     *        Ignored unless $mode is PEAR_ERROR_TRIGGER or
+     *        PEAR_ERROR_CALLBACK.  When $mode is PEAR_ERROR_TRIGGER,
+     *        this parameter is expected to be an integer and one of
+     *        E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR.  When
+     *        $mode is PEAR_ERROR_CALLBACK, this parameter is expected
+     *        to be the callback function or method.  A callback
+     *        function is a string with the name of the function, a
+     *        callback method is an array of two elements: the element
+     *        at index 0 is the object, and the element at index 1 is
+     *        the name of the method to call in the object.
+     *
+     * @see PEAR_ERROR_RETURN
+     * @see PEAR_ERROR_PRINT
+     * @see PEAR_ERROR_TRIGGER
+     * @see PEAR_ERROR_DIE
+     * @see PEAR_ERROR_CALLBACK
+     *
+     * @since PHP 4.0.5
+     */
+
+    function setErrorHandling($mode, $options = null)
+    {
+        if (isset($this)) {
+            $setmode = &$this->_default_error_mode;
+            $setoptions = &$this->_default_error_options;
+            $setcallback = &$this->_default_error_callback;
+        } else {
+            $setmode = &$GLOBALS['_PEAR_default_error_mode'];
+            $setoptions = &$GLOBALS['_PEAR_default_error_options'];
+            $setcallback = &$GLOBALS['_PEAR_default_error_callback'];
+        }
+
+        switch ($mode) {
+            case PEAR_ERROR_RETURN:
+            case PEAR_ERROR_PRINT:
+            case PEAR_ERROR_TRIGGER:
+            case PEAR_ERROR_DIE:
+            case null:
+                $setmode = $mode;
+                $setoptions = $options;
+                break;
+
+            case PEAR_ERROR_CALLBACK:
+                $setmode = $mode;
+                if (is_string($options) ||
+                    (is_array($options) && sizeof($options) == 2 &&
+                     is_object($options[0]) && is_string($options[1]))) {
+                    $setcallback = $options;
+                } else {
+                    trigger_error(E_USER_WARNING, "invalid error callback");
+                }
+                break;
+                    
+            default:
+                trigger_error(E_USER_WARNING, "invalid error mode");
+                break;
+        }
+    }
+
+    // }}}
+    // {{{ raiseError()
+
+    /**
+     * This method is called by DB to generate an error.
+     *
+     * @since PHP 4.0.5
+     */
+
+    function &raiseError($message = null, $code = null, $mode = null,
+                         $options = null, $userinfo = null)
+    {
+        if ($mode === null) {
+            $mode = $this->_default_error_mode;
+            if ($mode === null) {
+                $mode = $GLOBALS['_PEAR_default_error_mode'];
+            }
+        }
+
+        if ($mode == PEAR_ERROR_TRIGGER && $options === null) {
+            $options = $this->_default_error_options;
+            if ($options === null) {
+                $options = $GLOBALS['_PEAR_default_error_options'];
+            }
+        }
+
+        if ($mode == PEAR_ERROR_CALLBACK) {
+            if (!is_string($options) &&
+                !(is_array($options) && sizeof($options) == 2 &&
+                  is_object($options[0]) && is_string($options[1]))) {
+                $options = $this->_default_error_callback;
+                if ($options === null) {
+                    $options = $GLOBALS['_PEAR_default_error_callback'];
+                }
+            }
+        } else {
+            if ($options === null) {
+                $options = $this->_default_error_options;
+            }
+        }
+        return new PEAR_Error($message, $code, $mode, $options, $userinfo);
+    }
+
     // }}}
 }
 
@@ -176,11 +300,8 @@ class PEAR_Error
         * @access public
         *
         */
-       function PEAR_Error($message = 'unknown error',
-                                               $code = 0,
-                                               $mode = null,
-                                               $options = null,
-                                               $debuginfo = null)
+       function PEAR_Error($message = "unknown error", $code = null,
+                        $mode = null, $options = null, $userinfo = null)
        {
                if ($mode === null) {
                        $mode = PEAR_ERROR_RETURN;
@@ -188,7 +309,7 @@ class PEAR_Error
                $this->message   = $message;
                $this->code      = $code;
                $this->mode      = $mode;
-               $this->debuginfo = $debuginfo;
+               $this->userinfo = $userinfo;
                if ($mode & PEAR_ERROR_CALLBACK) {
                        $this->level = E_USER_NOTICE;
                        $this->callback = $options;
@@ -293,6 +414,20 @@ class PEAR_Error
                return get_class($this);
        }
 
+    // }}}
+    // {{{ getUserInfo()
+
+       /**
+        * Get additional user-supplied information.
+        *
+        * @return string user-supplied information
+        * @access public
+        */
+       function getUserInfo ()
+       {
+               return $this->userinfo;
+       }
+
     // }}}
     // {{{ getDebugInfo()
 
@@ -304,7 +439,7 @@ class PEAR_Error
         */
        function getDebugInfo ()
        {
-               return $this->debuginfo;
+               return $this->getUserInfo();
        }
 
     // }}}
index 53f599af5ea8bce0858e1731a134b32bec3fbf9a..9e59d3221f724d9df47890e3eeed8bb4029f91b0 100644 (file)
@@ -89,16 +89,16 @@ mode=print: test error[pear_error: message="test error" code=-42 mode=print leve
 mode=callback(function): errorhandler function called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorhandler prefix="" prepend="" append="" debug=""]
 mode=callback(method): errorhandler method called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorclass::errorhandler prefix="" prepend="" append="" debug=""]
 mode=print&trigger: test error<br>
-<b>Notice</b>:  test error in <b>PEAR.php</b> on line <b>206</b><br>
+<b>Notice</b>:  test error in <b>PEAR.php</b> on line <b>327</b><br>
 [pear_error: message="test error" code=-42 mode=print|trigger level=notice prefix="" prepend="" append="" debug=""]
 mode=trigger: <br>
-<b>Notice</b>:  test error in <b>PEAR.php</b> on line <b>206</b><br>
+<b>Notice</b>:  test error in <b>PEAR.php</b> on line <b>327</b><br>
 [pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append="" debug=""]
 mode=trigger,level=notice: <br>
-<b>Notice</b>:  test error in <b>PEAR.php</b> on line <b>206</b><br>
+<b>Notice</b>:  test error in <b>PEAR.php</b> on line <b>327</b><br>
 [pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append="" debug=""]
 mode=trigger,level=warning: <br>
-<b>Warning</b>:  test error in <b>PEAR.php</b> on line <b>206</b><br>
+<b>Warning</b>:  test error in <b>PEAR.php</b> on line <b>327</b><br>
 [pear_error: message="test error" code=-42 mode=trigger level=warning prefix="" prepend="" append="" debug=""]
 mode=trigger,level=error: <br>
-<b>Fatal error</b>:  test error in <b>PEAR.php</b> on line <b>206</b><br>
+<b>Fatal error</b>:  test error in <b>PEAR.php</b> on line <b>327</b><br>
diff --git a/pear/tests/pear_error3.phpt b/pear/tests/pear_error3.phpt
new file mode 100644 (file)
index 0000000..4703a22
--- /dev/null
@@ -0,0 +1,40 @@
+--TEST--
+PEAR default error handling
+--FILE--
+<?php // -*- C++ -*-
+
+// Test for: PEAR.php
+// Parts tested: - PEAR_Error class
+//               - PEAR::setErrorHandling
+//               - PEAR::raiseError method
+
+require_once "PEAR.php";
+
+error_reporting(4095);
+
+function errorhandler($eobj)
+{
+    if (PEAR::isError($eobj)) {
+        print "errorhandler called with an error object.\n";
+        print "error message: ".$eobj->getMessage()."\n";
+    } else {
+        print "errorhandler called, but without an error object.\n";
+    }
+}
+
+$obj = new PEAR;
+$obj->setErrorHandling(PEAR_ERROR_PRINT);
+$obj->raiseError("error 1\n");
+$obj->setErrorHandling(null);
+$obj->raiseError("error 2\n");
+PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, "errorhandler");
+$obj->raiseError("error 3\n");
+$obj->setErrorHandling(PEAR_ERROR_PRINT);
+$obj->raiseError("error 4\n");
+
+?>
+--EXPECT--
+error 1
+errorhandler called with an error object.
+error message: error 3
+error 4