ssb Mon Jan 29 16:55:27 2001 EDT Added files: /php4/pear/tests pear_error3.phpt Modified files: /php4/pear PEAR.php.in /php4/pear/tests pear_error.phpt Log: @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.
Index: php4/pear/PEAR.php.in diff -u php4/pear/PEAR.php.in:1.11 php4/pear/PEAR.php.in:1.12 --- php4/pear/PEAR.php.in:1.11 Tue Jan 9 17:01:52 2001 +++ php4/pear/PEAR.php.in Mon Jan 29 16:55:27 2001 @@ -17,7 +17,7 @@ // | Stig Bakken <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: PEAR.php.in,v 1.11 2001/01/10 01:01:52 ssb Exp $ +// $Id: PEAR.php.in,v 1.12 2001/01/30 00:55:27 ssb Exp $ // define('PEAR_ERROR_RETURN', 1); @@ -30,6 +30,9 @@ 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 @@ // {{{ properties var $_debug = false; + var $_default_error_mode = null; + var $_default_error_options = null; + var $_default_error_handler = ''; // }}} @@ -115,6 +121,124 @@ } // }}} + // {{{ 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 @@ * @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 @@ $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 @@ } // }}} + // {{{ getUserInfo() + + /** + * Get additional user-supplied information. + * + * @return string user-supplied information + * @access public + */ + function getUserInfo () + { + return $this->userinfo; + } + + // }}} // {{{ getDebugInfo() /** @@ -304,7 +439,7 @@ */ function getDebugInfo () { - return $this->debuginfo; + return $this->getUserInfo(); } // }}} Index: php4/pear/tests/pear_error.phpt diff -u php4/pear/tests/pear_error.phpt:1.4 php4/pear/tests/pear_error.phpt:1.5 --- php4/pear/tests/pear_error.phpt:1.4 Thu Dec 7 11:53:27 2000 +++ php4/pear/tests/pear_error.phpt Mon Jan 29 16:55:27 2001 @@ -89,16 +89,16 @@ 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> Index: php4/pear/tests/pear_error3.phpt +++ php4/pear/tests/pear_error3.phpt --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
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]