From 7cf00c707615ba12074654f9f9297cf2778a068c Mon Sep 17 00:00:00 2001 From: Stig Bakken Date: Tue, 30 Jan 2001 00:55:27 +0000 Subject: [PATCH] @Added raiseError and setErrorHandling methods to PEAR class (Stig, PEAR) # This allows all objects to have their own default error handling # or use a global default. --- pear/PEAR.php.in | 149 ++++++++++++++++++++++++++++++++++-- pear/tests/pear_error.phpt | 10 +-- pear/tests/pear_error3.phpt | 40 ++++++++++ 3 files changed, 187 insertions(+), 12 deletions(-) create mode 100644 pear/tests/pear_error3.phpt diff --git a/pear/PEAR.php.in b/pear/PEAR.php.in index 88d3ca474c..4f7fe166c9 100644 --- a/pear/PEAR.php.in +++ b/pear/PEAR.php.in @@ -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(); } // }}} diff --git a/pear/tests/pear_error.phpt b/pear/tests/pear_error.phpt index 53f599af5e..9e59d3221f 100644 --- a/pear/tests/pear_error.phpt +++ b/pear/tests/pear_error.phpt @@ -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
-Notice: test error in PEAR.php on line 206
+Notice: test error in PEAR.php on line 327
[pear_error: message="test error" code=-42 mode=print|trigger level=notice prefix="" prepend="" append="" debug=""] mode=trigger:
-Notice: test error in PEAR.php on line 206
+Notice: test error in PEAR.php on line 327
[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append="" debug=""] mode=trigger,level=notice:
-Notice: test error in PEAR.php on line 206
+Notice: test error in PEAR.php on line 327
[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append="" debug=""] mode=trigger,level=warning:
-Warning: test error in PEAR.php on line 206
+Warning: test error in PEAR.php on line 327
[pear_error: message="test error" code=-42 mode=trigger level=warning prefix="" prepend="" append="" debug=""] mode=trigger,level=error:
-Fatal error: test error in PEAR.php on line 206
+Fatal error: test error in PEAR.php on line 327
diff --git a/pear/tests/pear_error3.phpt b/pear/tests/pear_error3.phpt new file mode 100644 index 0000000000..4703a222b6 --- /dev/null +++ b/pear/tests/pear_error3.phpt @@ -0,0 +1,40 @@ +--TEST-- +PEAR default error handling +--FILE-- +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 -- 2.50.1