diff options
author | Stig Bakken <ssb@php.net> | 2001-01-30 00:55:27 +0000 |
---|---|---|
committer | Stig Bakken <ssb@php.net> | 2001-01-30 00:55:27 +0000 |
commit | 7cf00c707615ba12074654f9f9297cf2778a068c (patch) | |
tree | 6ee6a42db0ac5d5e7c3472059e70f8e2b6873858 | |
parent | bbca5a86f868a37561c373fe1819a48e1c480e54 (diff) | |
download | php-git-7cf00c707615ba12074654f9f9297cf2778a068c.tar.gz |
@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.
-rw-r--r-- | pear/PEAR.php.in | 149 | ||||
-rw-r--r-- | pear/tests/pear_error.phpt | 10 | ||||
-rw-r--r-- | pear/tests/pear_error3.phpt | 40 |
3 files changed, 187 insertions, 12 deletions
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 = ''; // }}} @@ -115,6 +121,124 @@ class PEAR } // }}} + // {{{ 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); + } + + // }}} } // {{{ _PEAR_call_destructors() @@ -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; @@ -294,6 +415,20 @@ class PEAR_Error } // }}} + // {{{ 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<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 index 0000000000..4703a222b6 --- /dev/null +++ b/pear/tests/pear_error3.phpt @@ -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 |