* make the error class used by raiseError configurable (_error_class property,

overridden by first parameter to constructor)
This commit is contained in:
Stig Bakken 2001-04-16 21:45:21 +00:00
parent d3bb832585
commit ee17aae3ac
2 changed files with 71 additions and 22 deletions

View File

@ -40,6 +40,14 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
define('PEAR_OS', 'Unix'); // blatant assumption
}
if (!defined("DIRECTORY_SEPARATOR")) {
if (OS_WINDOWS) {
define("DIRECTORY_SEPARATOR", "\\");
} else {
define("DIRECTORY_SEPARATOR", "/");
}
}
$_PEAR_default_error_mode = PEAR_ERROR_RETURN;
$_PEAR_default_error_options = E_USER_NOTICE;
$_PEAR_default_error_callback = '';
@ -76,6 +84,7 @@ class PEAR
var $_default_error_mode = null;
var $_default_error_options = null;
var $_default_error_handler = '';
var $_error_class = 'PEAR_Error';
// }}}
@ -86,12 +95,15 @@ class PEAR
* $_PEAR_destructor_object_list for destructor emulation if a
* destructor object exists.
*/
function PEAR()
function PEAR($error_class = null)
{
$classname = get_class($this);
if ($this->_debug) {
print "PEAR constructor called, class=$classname\n";
}
if ($error_class !== null) {
$this->_error_class = $error_class;
}
while ($classname) {
$destructor = "_$classname";
if (method_exists($this, $destructor)) {
@ -219,10 +231,10 @@ class PEAR
// {{{ raiseError()
/**
* This method is a wrapper that returns an instance of PEAR_Error
* with this object's default error handling applied. If the
* $mode and $options parameters are not specified, the object's
* defaults are used.
* This method is a wrapper that returns an instance of the
* configured error class with this object's default error
* handling applied. If the $mode and $options parameters are not
* specified, the object's defaults are used.
*
* @param $message a text error message
* @param $code a numeric error code (it is up to your class
@ -285,7 +297,8 @@ class PEAR
}
}
}
return new PEAR_Error($message, $code, $mode, $options, $userinfo);
$ec = $this->_error_class;
return new $ec($message, $code, $mode, $options, $userinfo);
}
// }}}
@ -501,6 +514,18 @@ class PEAR_Error
return $this->getUserInfo();
}
// }}}
// {{{ addUserInfo()
function addUserInfo($info)
{
if (empty($this->userinfo)) {
$this->userinfo = $info;
} else {
$this->userinfo .= " ** $info";
}
}
// }}}
// {{{ toString()
@ -524,7 +549,7 @@ class PEAR_Error
}
return sprintf('[%s: message="%s" code=%d mode=callback '.
'callback=%s prefix="%s" prepend="%s" append="%s" '.
'debug="%s"]',
'info="%s"]',
get_class($this), $this->message, $this->code,
$callback, $this->error_message_prefix,
$this->error_prepend, $this->error_append,

View File

@ -13,6 +13,24 @@ require_once "PEAR.php";
error_reporting(4095);
class Foo_Error extends PEAR_Error {
function Foo_Error($message = "unknown error", $code = null,
$mode = null, $options = null, $userinfo = null)
{
$this->PEAR_Error($message, $code, $mode, $options, $userinfo);
$this->error_message_prefix = 'Foo_Error prefix';
}
}
class Test1 extends PEAR {
function Test1() {
$this->PEAR("Foo_Error");
}
function runtest() {
return $this->raiseError("test error");
}
}
function errorhandler(&$obj) {
print "errorhandler function called, obj=".$obj->toString()."\n";
}
@ -23,6 +41,11 @@ class errorclass {
}
}
print "specify error class: ";
$obj = new Test1;
$err = $obj->runtest();
print $err->toString() . "\n";
$eo = new errorclass;
print "default PEAR_Error: ";
@ -79,26 +102,27 @@ print $err->toString() . "\n";
--GET--
--POST--
--EXPECT--
default PEAR_Error: [pear_error: message="unknown error" code=0 mode=return level=notice prefix="" prepend="" append="" debug=""]
specify error class: [foo_error: message="test error" code=0 mode=return level=notice prefix="Foo_Error prefix" prepend="" append="" info=""]
default PEAR_Error: [pear_error: message="unknown error" code=0 mode=return level=notice prefix="" prepend="" append="" info=""]
Testing it: bool(true)
This is not an error: bool(false)
Now trying a bunch of variations...
different message: [pear_error: message="test error" code=0 mode=return level=notice prefix="" prepend="" append="" debug=""]
different message,code: [pear_error: message="test error" code=-42 mode=return level=notice prefix="" prepend="" append="" debug=""]
mode=print: test error[pear_error: message="test error" code=-42 mode=print level=notice prefix="" prepend="" append="" debug=""]
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=""]
different message: [pear_error: message="test error" code=0 mode=return level=notice prefix="" prepend="" append="" info=""]
different message,code: [pear_error: message="test error" code=-42 mode=return level=notice prefix="" prepend="" append="" info=""]
mode=print: test error[pear_error: message="test error" code=-42 mode=print level=notice prefix="" prepend="" append="" info=""]
mode=callback(function): errorhandler function called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorhandler prefix="" prepend="" append="" info=""]
mode=callback(method): errorhandler method called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorclass::errorhandler prefix="" prepend="" append="" info=""]
mode=print&trigger: test error<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=""]
<b>Notice</b>: test error in <b>/usr/local/lib/php/PEAR.php</b> on line <b>399</b><br>
[pear_error: message="test error" code=-42 mode=print|trigger level=notice prefix="" prepend="" append="" info=""]
mode=trigger: <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=""]
<b>Notice</b>: test error in <b>/usr/local/lib/php/PEAR.php</b> on line <b>399</b><br>
[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append="" info=""]
mode=trigger,level=notice: <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=""]
<b>Notice</b>: test error in <b>/usr/local/lib/php/PEAR.php</b> on line <b>399</b><br>
[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append="" info=""]
mode=trigger,level=warning: <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=""]
<b>Warning</b>: test error in <b>/usr/local/lib/php/PEAR.php</b> on line <b>399</b><br>
[pear_error: message="test error" code=-42 mode=trigger level=warning prefix="" prepend="" append="" info=""]
mode=trigger,level=error: <br>
<b>Fatal error</b>: test error in <b>PEAR.php</b> on line <b>327</b><br>
<b>Fatal error</b>: test error in <b>/usr/local/lib/php/PEAR.php</b> on line <b>399</b><br>