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();
//
// {{{ properties
var $_debug = false;
+ var $_default_error_mode = null;
+ var $_default_error_options = null;
+ var $_default_error_handler = '';
// }}}
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);
+ }
+
// }}}
}
* @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;
$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;
return get_class($this);
}
+ // }}}
+ // {{{ getUserInfo()
+
+ /**
+ * Get additional user-supplied information.
+ *
+ * @return string user-supplied information
+ * @access public
+ */
+ function getUserInfo ()
+ {
+ return $this->userinfo;
+ }
+
// }}}
// {{{ getDebugInfo()
*/
function getDebugInfo ()
{
- return $this->debuginfo;
+ return $this->getUserInfo();
}
// }}}
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>
--- /dev/null
+--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