* If you want a destructor in your class, inherit PEAR and make a
* destructor method called _yourclassname (same name as the
* constructor, but with a "_" prefix). Also, in your constructor you
- * have to call the PEAR constructor: <code>$this->PEAR();</code>.
+ * have to call the PEAR constructor: $this->PEAR();.
* The destructor method will be called without parameters. Note that
* at in some SAPI implementations (such as Apache), any output during
* the request shutdown (in which destructors are called) seems to be
* discarded. If you need to get any debug information from your
- * destructor, use <code>error_log()</code>, <code>syslog()</code> or
- * something like that instead.
+ * destructor, use error_log(), syslog() or something similar.
*
* @since PHP 4.0.2
* @author Stig Bakken <ssb@fast.no>
* @var array
* @access private
*/
- var $_expected_errors = null;
+ var $_expected_errors = array();
// }}}
* 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,
+ * @param int $mode
+ * 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
+ * @param mixed $options
+ * When $mode is PEAR_ERROR_TRIGGER, this is the error level (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.
*
+ * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
+ * a printf format string used when printing the error
+ * message.
+ *
* @access public
* @return void
* @see PEAR_ERROR_RETURN
/**
* This method is used to tell which errors you expect to get.
* Expected errors are always returned with error mode
- * PEAR_ERROR_RETURN. To stop expecting errors, call this method
- * again without parameters.
+ * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
+ * and this method pushes a new element onto it. The list of
+ * expected errors are in effect until they are popped off the
+ * stack with the popExpect() method.
*
* @param mixed a single error code or an array of error codes
* to expect
*
- * @return void
+ * @return int the new depth of the "expected errors" stack
*/
- function expectError($code = null)
+ function expectError($code)
{
- if ($code === null || is_array($code)) {
- $this->_expected_errors = $code;
+ if (is_array($code)) {
+ array_push($this->_expected_errors, $code);
} else {
- $this->_expected_errors = array($code);
+ array_push($this->_expected_errors, array($code));
}
+ return sizeof($this->_expected_errors);
+ }
+
+ // }}}
+ // {{{ popExpect()
+
+ /**
+ * This method pops one element off the expected error codes
+ * stack.
+ *
+ * @return array the list of error codes that were popped
+ */
+ function popExpect()
+ {
+ return array_pop($this->_expected_errors);
}
// }}}
$message = $message->getMessage();
}
- if (@in_array($code, $this->_expected_errors)) {
+ if (sizeof($this->_expected_errors) > 0 && in_array($code, end($this->_expected_errors))) {
$mode = PEAR_ERROR_RETURN;
}
$this->callback = null;
}
if ($this->mode & PEAR_ERROR_PRINT) {
- print $this->getMessage();
+ if (is_null($options) || is_int($options)) {
+ $format = "%s";
+ } else {
+ $format = $options;
+ }
+ printf($format, $this->getMessage());
}
if ($this->mode & PEAR_ERROR_TRIGGER) {
trigger_error($this->getMessage(), $this->level);
}
if ($this->mode & PEAR_ERROR_DIE) {
$msg = $this->getMessage();
- if (substr($msg, -1) != "\n") {
- $msg .= "\n";
+ if (is_null($options) || is_int($options)) {
+ $format = "%s";
+ if (substr($msg, -1) != "\n") {
+ $msg .= "\n";
+ }
+ } else {
+ $format = $options;
}
- die($msg);
+ die(sprintf($format, $msg));
}
if ($this->mode & PEAR_ERROR_CALLBACK) {
if (is_string($this->callback) && strlen($this->callback)) {
/*
* Local Variables:
- * mode: c++
+ * mode: php
* tab-width: 4
* c-basic-offset: 4
* End:
PEAR_Error test
--SKIPIF--
--FILE--
-<?php // -*- C++ -*-
+<?php // -*- PHP -*-
// Test for: PEAR.php
// Parts tested: - PEAR_Error class
// - PEAR::isError static method
-// testing PEAR_Error
require_once "../PEAR.php";
-error_reporting(4095);
+function test_error_handler($errno, $errmsg, $file, $line, $vars) {
+ $errortype = array (
+ 1 => "Error",
+ 2 => "Warning",
+ 4 => "Parsing Error",
+ 8 => "Notice",
+ 16 => "Core Error",
+ 32 => "Core Warning",
+ 64 => "Compile Error",
+ 128 => "Compile Warning",
+ 256 => "User Error",
+ 512 => "User Warning",
+ 1024=> "User Notice"
+ );
+ $prefix = $errortype[$errno];
+ $file = basename($file);
+ print "\n$prefix: $errmsg in $file on line $line\n";
+}
+
+error_reporting(E_ALL);
+set_error_handler("test_error_handler");
-class Foo_Error extends PEAR_Error {
+class Foo_Error extends PEAR_Error
+{
function Foo_Error($message = "unknown error", $code = null,
$mode = null, $options = null, $userinfo = null)
{
class errorclass {
function errorhandler(&$obj) {
- print "errorhandler method called, obj=".$obj->toString()."\n";
+ print "errorhandler method called, obj=".$obj->toString()."\n";
}
}
$err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER, E_USER_ERROR);
print $err->toString() . "\n";
+print "testing expectError:\n";
+$obj =& new PEAR;
+$obj->setErrorHandling(PEAR_ERROR_PRINT, "*** ERROR: %s\n");
+print "expecting syntax/invalid\n";
+$obj->expectError(array("syntax", "invalid"));
+print "raising already_exists\n";
+$err = $obj->raiseError("already_exists");
+print "raising syntax\n";
+$err = $obj->raiseError("syntax");
+print "expecting syntax only\n";
+$obj->expectError(array("syntax"));
+print "raising invalid\n";
+$err = $obj->raiseError("invalid");
+print "popping\n";
+var_dump($obj->popExpect());
+print "raising invalid\n";
+$err = $obj->raiseError("invalid");
+print "popping\n";
+var_dump($obj->popExpect());
+print "raising invalid\n";
+$err = $obj->raiseError("invalid");
+
?>
--GET--
--POST--
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>/home/ssb/cvs/php/php4/pear/PEAR.php</b> on line <b>567</b><br>
+mode=print&trigger: test error
+User Notice: test error in PEAR.php on line 591
[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>/home/ssb/cvs/php/php4/pear/PEAR.php</b> on line <b>567</b><br>
+mode=trigger:
+User Notice: test error in PEAR.php on line 591
[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>/home/ssb/cvs/php/php4/pear/PEAR.php</b> on line <b>567</b><br>
+mode=trigger,level=notice:
+User Notice: test error in PEAR.php on line 591
[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>/home/ssb/cvs/php/php4/pear/PEAR.php</b> on line <b>567</b><br>
+mode=trigger,level=warning:
+User Warning: test error in PEAR.php on line 591
[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>/home/ssb/cvs/php/php4/pear/PEAR.php</b> on line <b>567</b><br>
+mode=trigger,level=error:
+User Error: test error in PEAR.php on line 591
+[pear_error: message="test error" code=-42 mode=trigger level=error prefix="" prepend="" append="" info=""]
+testing expectError:
+expecting syntax/invalid
+raising already_exists
+*** ERROR: already_exists
+raising syntax
+*** ERROR: syntax
+expecting syntax only
+raising invalid
+*** ERROR: invalid
+popping
+array(1) {
+ [0]=>
+ string(6) "syntax"
+}
+raising invalid
+*** ERROR: invalid
+popping
+array(2) {
+ [0]=>
+ string(6) "syntax"
+ [1]=>
+ string(7) "invalid"
+}
+raising invalid
+*** ERROR: invalid