From 7c53e635b27732ba8404ed593c7aa418f870ec63 Mon Sep 17 00:00:00 2001 From: Stig Bakken Date: Wed, 13 Sep 2000 11:27:59 +0000 Subject: [PATCH] PEAR: * added $debuginfo parameter to PEAR_Error constructor * put last query in $debuginfo from DB_common::raiseError() @PEAR: add last executed query as debug info in DB errors (Stig) --- pear/DB.php | 22 +++++++++++++++------- pear/PEAR.php.in | 38 ++++++++++++++++++++++++++++---------- pear/tests/pear_error.phpt | 30 +++++++++++++++--------------- 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/pear/DB.php b/pear/DB.php index d14c2b21d6b..37c1ed0b168 100644 --- a/pear/DB.php +++ b/pear/DB.php @@ -469,17 +469,21 @@ class DB_Error extends PEAR_Error { * * @param $code mixed DB error code, or string with error message. * @param $mode int what "error mode" to operate in - * @param $level what error level to use for $mode == PEAR_ERROR_TRIGGER + * @param $level what error level to use for $mode & PEAR_ERROR_TRIGGER + * @param $debuginfo additional debug info, such as the last query * * @access public + * + * @see PEAR_Error */ function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN, - $level = E_USER_NOTICE) { + $level = E_USER_NOTICE, + $debuginfo = null) { if (is_int($code)) { - $this->PEAR_Error("DB Error: " . DB::errorMessage($code), $code, $mode, $level); + $this->PEAR_Error("DB Error: " . DB::errorMessage($code), $code, $mode, $level, $debuginfo); } else { - $this->PEAR_Error("DB Error: $code", 0, $mode, $level); + $this->PEAR_Error("DB Error: $code", 0, $mode, $level, $debuginfo); } } } @@ -497,16 +501,20 @@ class DB_Warning extends PEAR_Error { * @param $code mixed DB error code, or string with error message. * @param $mode int what "error mode" to operate in * @param $level what error level to use for $mode == PEAR_ERROR_TRIGGER + * @param $debuginfo additional debug info, such as the last query * * @access public + * + * @see PEAR_Error */ function DB_Warning($code = DB_WARNING, $mode = PEAR_ERROR_RETURN, - $level = E_USER_NOTICE) { + $level = E_USER_NOTICE, + $debuginfo = null) { if (is_int($code)) { - $this->PEAR_Error("DB Warning: " . DB::errorMessage($code), $code, $mode, $level); + $this->PEAR_Error("DB Warning: " . DB::errorMessage($code), $code, $mode, $level, $debuginfo); } else { - $this->PEAR_Error("DB Warning: $code", 0, $mode, $level); + $this->PEAR_Error("DB Warning: $code", 0, $mode, $level, $debuginfo); } } } diff --git a/pear/PEAR.php.in b/pear/PEAR.php.in index 92f48986c28..3a6770cafe4 100644 --- a/pear/PEAR.php.in +++ b/pear/PEAR.php.in @@ -144,12 +144,11 @@ class PEAR_Error var $error_message_prefix = ''; var $error_prepend = ''; var $error_append = ''; - var $mode = PEAR_ERROR_RETURN; var $level = E_USER_NOTICE; var $code = -1; - var $message = ''; + var $debuginfo = ''; // Wait until we have a stack-groping function in PHP. //var $file = ''; @@ -178,14 +177,16 @@ class PEAR_Error function PEAR_Error($message = 'unknown error', $code = 0, $mode = null, - $options = null) + $options = null, + $debuginfo = null) { if ($mode === null) { $mode = PEAR_ERROR_RETURN; } - $this->message = $message; - $this->code = $code; - $this->mode = $mode; + $this->message = $message; + $this->code = $code; + $this->mode = $mode; + $this->debuginfo = $debuginfo; if ($mode & PEAR_ERROR_CALLBACK) { $this->level = E_USER_NOTICE; $this->callback = $options; @@ -275,6 +276,19 @@ class PEAR_Error return get_class($this); } + // }}} + // {{{ getDebugInfo() + + /** + * Get additional debug information supplied by the application. + * + * @return string debug information + */ + function getDebugInfo () + { + return $this->debuginfo; + } + // }}} // {{{ toString() @@ -296,10 +310,12 @@ class PEAR_Error $callback = $this->callback; } return sprintf('[%s: message="%s" code=%d mode=callback '. - 'callback=%s prefix="%s" prepend="%s" append="%s"]', + 'callback=%s prefix="%s" prepend="%s" append="%s" '. + 'debug="%s"]', get_class($this), $this->message, $this->code, $callback, $this->error_message_prefix, - $this->error_prepend, $this->error_append); + $this->error_prepend, $this->error_append, + $this->debuginfo); } if ($this->mode & PEAR_ERROR_CALLBACK) { $modes[] = "callback"; @@ -316,11 +332,13 @@ class PEAR_Error if ($this->mode & PEAR_ERROR_RETURN) { $modes[] = "return"; } - return sprintf('[%s: message="%s" code=%d mode=%s level=%s prefix="%s" prepend="%s" append="%s"]', + return sprintf('[%s: message="%s" code=%d mode=%s level=%s prefix="%s" '. + 'prepend="%s" append="%s" debug="%s"]', get_class($this), $this->message, $this->code, implode("|", $modes), $levels[$this->level], $this->error_message_prefix, - $this->error_prepend, $this->error_append); + $this->error_prepend, $this->error_append, + $this->debuginfo); } // }}} diff --git a/pear/tests/pear_error.phpt b/pear/tests/pear_error.phpt index 6fe45f4eebe..6c88b803da8 100644 --- a/pear/tests/pear_error.phpt +++ b/pear/tests/pear_error.phpt @@ -79,26 +79,26 @@ print $err->toString() . "\n"; --GET-- --POST-- --EXPECT-- -default PEAR_Error: [pear_error: message="unknown error" code=0 mode=return level=notice prefix="" prepend="" append=""] +default PEAR_Error: [pear_error: message="unknown error" code=0 mode=return level=notice prefix="" prepend="" append="" debug=""] 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=""] -different message,code: [pear_error: message="test error" code=-42 mode=return level=notice prefix="" prepend="" append=""] -mode=print: test error[pear_error: message="test error" code=-42 mode=print level=notice prefix="" prepend="" append=""] -mode=callback(function): errorhandler function called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorhandler prefix="" prepend="" append=""] -mode=callback(method): errorhandler method called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorclass::errorhandler prefix="" prepend="" append=""] +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=""] mode=print&trigger: test error
-Notice: test error in PEAR.php on line 203
-[pear_error: message="test error" code=-42 mode=print|trigger level=notice prefix="" prepend="" append=""] +Notice: test error in PEAR.php on line 204
+[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 203
-[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] +Notice: test error in PEAR.php on line 204
+[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 203
-[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] +Notice: test error in PEAR.php on line 204
+[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 203
-[pear_error: message="test error" code=-42 mode=trigger level=warning prefix="" prepend="" append=""] +Warning: test error in PEAR.php on line 204
+[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 203
+Fatal error: test error in PEAR.php on line 204