From: Stig Bakken Date: Sat, 9 Sep 2000 02:39:56 +0000 (+0000) Subject: @Added a more configurable error reporting interface to DB. X-Git-Tag: php-4.0.3RC1~232 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=971f8b08811b1dfef47841dd63c3ce349959e64f;p=php @Added a more configurable error reporting interface to DB. Added a more configurable error reporting interface to DB. Also added some more tests, and moved the DB tests to pear/DB/tests. #Usage example that prints and exits on every error: #$dbh = DB::connect($dsn); #$dbh->setErrorHandling(PEAR_ERROR_DIE); # #Example with plain callback function: #$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, "errorHandler"); # #Example with object callback function: #$dbh->setErrorHandling(PEAR_ERROR_CALLBACK, array($obj, "errorHandler")); # #Handler functions/methods are called with the error object as a parameter. # --- diff --git a/pear/PEAR.php.in b/pear/PEAR.php.in index a4cb0d42d4..dbee200037 100644 --- a/pear/PEAR.php.in +++ b/pear/PEAR.php.in @@ -24,6 +24,7 @@ define('PEAR_ERROR_RETURN', 0); define('PEAR_ERROR_PRINT', 1); define('PEAR_ERROR_TRIGGER', 2); define('PEAR_ERROR_DIE', 3); +define('PEAR_ERROR_CALLBACK', 4); define('PHP_BINDIR', '@prefix@/bin'); define('PEAR_INSTALL_DIR', '@PEAR_INSTALLDIR@'); @@ -91,6 +92,10 @@ class PEAR * @access public */ function _PEAR() { + if ($this->_debug) { + printf("PEAR destructor called, class=%s\n", + get_class($this)); + } } // }}} @@ -136,7 +141,6 @@ class PEAR_Error { // {{{ properties - var $classname = ''; var $error_message_prefix = ''; var $error_prepend = ''; var $error_append = ''; @@ -159,8 +163,17 @@ class PEAR_Error * PEAR_Error constructor * * @param $message error message + * * @param $code (optional) error code * + * @param $mode (optional) error mode, one of: PEAR_ERROR_RETURN, + * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER or + * PEAR_ERROR_CALLBACK + * + * @param $level (optional) error level, _OR_ in the case of + * PEAR_ERROR_CALLBACK, the callback function or object/method + * tuple. + * */ function PEAR_Error($message = 'unknown error', $code = 0, @@ -170,9 +183,12 @@ class PEAR_Error $this->message = $message; $this->code = $code; $this->mode = $mode; - $this->level = $level; - if (!$this->classname) { - $this->classname = get_class($this); + if ($mode == PEAR_ERROR_CALLBACK) { + $this->level = E_USER_NOTICE; + $this->callback = $level; + } else { + $this->level = $level; + $this->callback = false; } switch ($this->mode) { case PEAR_ERROR_PRINT: @@ -184,12 +200,38 @@ class PEAR_Error case PEAR_ERROR_DIE: die($this->getMessage()); break; + case PEAR_ERROR_CALLBACK: + if (is_string($this->callback) && strlen($this->callback)) { + call_user_func($this->callback, $this); + } elseif (is_array($this->callback) && + sizeof($this->callback) == 2 && + is_object($this->callback[0]) && + is_string($this->callback[1]) && + strlen($this->callback[1])) { + call_user_method($this->callback[1], $this->callback[0], + $this); + } + break; case PEAR_ERROR_RETURN: default: break; } } + // }}} + // {{{ getMode() + + function getMode() { + return $this->mode; + } + + // }}} + // {{{ getCallback() + + function getCallback() { + return $this->callback; + } + // }}} // {{{ getMessage() @@ -214,9 +256,9 @@ class PEAR_Error * * @return int error code */ - function getCode () + function getCode() { - return ($this->code); + return $this->code; } // }}} @@ -229,7 +271,7 @@ class PEAR_Error */ function getType () { - return $this->classname; + return get_class($this); } // }}} @@ -244,7 +286,8 @@ class PEAR_Error $modes = array(PEAR_ERROR_RETURN => "return", PEAR_ERROR_PRINT => "print", PEAR_ERROR_TRIGGER => "trigger", - PEAR_ERROR_DIE => "die"); + PEAR_ERROR_DIE => "die", + PEAR_ERROR_CALLBACK => "callback"); $levels = array(E_USER_NOTICE => "notice", E_USER_WARNING => "warning", E_USER_ERROR => "error"); diff --git a/pear/tests/DB_Error.phpt b/pear/tests/DB_Error.phpt deleted file mode 100644 index 4589df504d..0000000000 --- a/pear/tests/DB_Error.phpt +++ /dev/null @@ -1,77 +0,0 @@ ---TEST-- -DB_Error/DB_Warning test ---SKIPIF-- - ---FILE-- -toString()."\n"; -$e = new DB_Error("test error"); print $e->toString()."\n"; -$e = new DB_Error(DB_OK); print $e->toString()."\n"; -$e = new DB_Error(DB_ERROR); print $e->toString()."\n"; -$e = new DB_Error(DB_ERROR_SYNTAX); print $e->toString()."\n"; -$e = new DB_Error(DB_ERROR_DIVZERO); print $e->toString()."\n"; -$e = new DB_Warning(); print $e->toString()."\n"; -$e = new DB_Warning("test warning"); print $e->toString()."\n"; -$e = new DB_Warning(DB_WARNING_READ_ONLY); print $e->toString()."\n"; - -ini_alter("html_errors", false); - -print "testing different error modes...\n"; -$e = new DB_Error(DB_ERROR, PEAR_ERROR_PRINT); print $e->toString()."\n"; -$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER); - -print "testing different error serverities...\n"; -$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER, E_USER_NOTICE); -$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER, E_USER_WARNING); -$e = new DB_Error(DB_ERROR_SYNTAX, PEAR_ERROR_TRIGGER, E_USER_ERROR); - -?> ---GET-- ---POST-- ---EXPECT-- -testing different error codes... -[db_error: message="DB Error: unknown error" code=-1 mode=return level=notice prefix="" prepend="" append=""] -[db_error: message="DB Error: test error" code=0 mode=return level=notice prefix="" prepend="" append=""] -[db_error: message="DB Error: no error" code=0 mode=return level=notice prefix="" prepend="" append=""] -[db_error: message="DB Error: unknown error" code=-1 mode=return level=notice prefix="" prepend="" append=""] -[db_error: message="DB Error: syntax error" code=-2 mode=return level=notice prefix="" prepend="" append=""] -[db_error: message="DB Error: division by zero" code=-13 mode=return level=notice prefix="" prepend="" append=""] -[db_warning: message="DB Warning: unknown warning" code=-1000 mode=return level=notice prefix="" prepend="" append=""] -[db_warning: message="DB Warning: test warning" code=0 mode=return level=notice prefix="" prepend="" append=""] -[db_warning: message="DB Warning: read only" code=-1001 mode=return level=notice prefix="" prepend="" append=""] -testing different error modes... -DB Error: unknown error[db_error: message="DB Error: unknown error" code=-1 mode=print level=notice prefix="" prepend="" append=""] -
-Notice: DB Error: syntax error in PEAR.php on line 182
-testing different error serverities... -
-Notice: DB Error: syntax error in PEAR.php on line 182
-
-Warning: DB Error: syntax error in PEAR.php on line 182
-
-Fatal error: DB Error: syntax error in PEAR.php on line 182
diff --git a/pear/tests/DB_factory.phpt b/pear/tests/DB_factory.phpt deleted file mode 100644 index f0a15e8e6e..0000000000 --- a/pear/tests/DB_factory.phpt +++ /dev/null @@ -1,46 +0,0 @@ ---TEST-- -DB::factory test ---SKIPIF-- - ---FILE-- -getMessage()."\n"; - } else { - print "object: ".$obj."\n"; - } -} - -?> ---GET-- ---POST-- ---EXPECT-- -testing ibase: object: db_ibase: (phptype=ibase, dbsyntax=ibase) -testing msql: object: db_msql: (phptype=msql, dbsyntax=msql) -testing mssql: object: db_mssql: (phptype=mssql, dbsyntax=mssql) -testing mysql: object: db_mysql: (phptype=mysql, dbsyntax=mysql) -testing oci8: object: db_oci8: (phptype=oci8, dbsyntax=oci8) -testing odbc: object: db_odbc: (phptype=odbc, dbsyntax=unknown) -testing pgsql: object: db_pgsql: (phptype=pgsql, dbsyntax=pgsql) -testing sybase: object: db_sybase: (phptype=sybase, dbsyntax=sybase) diff --git a/pear/tests/DB_parseDSN.phpt b/pear/tests/DB_parseDSN.phpt deleted file mode 100644 index a4c969eb95..0000000000 --- a/pear/tests/DB_parseDSN.phpt +++ /dev/null @@ -1,38 +0,0 @@ ---TEST-- -DB::parseDSN test ---SKIPIF-- - ---FILE-- - ---GET-- ---POST-- ---EXPECT-- -testing DB::parseDSN... -mysql,,,,,, -odbc,mssql,,,,, -mysql,mysql,,localhost,,, -mysql,mysql,,remote.host.com,db,, -mysql,mysql,,,,testuser,testpw -oci8,oci8,,tns-name,,user,pass -odbc,solid,tcp,localhost 1313,,foo,bar diff --git a/pear/tests/pear1.phpt b/pear/tests/pear1.phpt new file mode 100644 index 0000000000..2dc49a656d --- /dev/null +++ b/pear/tests/pear1.phpt @@ -0,0 +1,39 @@ +--TEST-- +PEAR constructor/destructor test +--SKIPIF-- +--FILE-- +_debug = true; + $this->name = $name; + $this->PEAR(); + } + function _TestPEAR() { + print "This is the TestPEAR($this->name) destructor\n"; + $this->_PEAR(); + } +} + +print "test class TestPEAR\n"; +$o = new TestPEAR("test1"); +$p = new TestPEAR("test2"); +var_dump(get_class($o)); +var_dump(get_class($p)); + +?> +--GET-- +--POST-- +--EXPECT-- +test class TestPEAR +PEAR constructor called, class=testpear +PEAR constructor called, class=testpear +string(8) "testpear" +string(8) "testpear" +This is the TestPEAR(test1) destructor +PEAR destructor called, class=testpear +This is the TestPEAR(test2) destructor +PEAR destructor called, class=testpear diff --git a/pear/tests/pear_error.phpt b/pear/tests/pear_error.phpt new file mode 100644 index 0000000000..851900cfe9 --- /dev/null +++ b/pear/tests/pear_error.phpt @@ -0,0 +1,69 @@ +--TEST-- +PEAR_Error test +--SKIPIF-- +--FILE-- +toString() . "\n"; +print "Testing it: "; +var_dump(PEAR::isError($err)); +print "This is not an error: "; +$str = "not an error"; +var_dump(PEAR::isError($str)); + +print "Now trying a bunch of variations...\n"; +print "different message: "; +$err = new PEAR_Error("test error"); +print $err->toString() . "\n"; +print "different message,code: "; +$err = new PEAR_Error("test error", -42); +print $err->toString() . "\n"; +print "mode=print: "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_PRINT); +print $err->toString() . "\n"; +print "mode=trigger: "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER); +print $err->toString() . "\n"; +print "mode=trigger,level=notice: "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER, E_USER_NOTICE); +print $err->toString() . "\n"; +print "mode=trigger,level=warning: "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER, E_USER_WARNING); +print $err->toString() . "\n"; +print "mode=trigger,level=error: "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER, E_USER_ERROR); +print $err->toString() . "\n"; + +?> +--GET-- +--POST-- +--EXPECT-- +default PEAR_Error: [pear_error: message="unknown error" code=0 mode=return level=notice prefix="" prepend="" append=""] +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=trigger:
+Notice: test error in PEAR.php on line 198
+[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] +mode=trigger,level=notice:
+Notice: test error in PEAR.php on line 198
+[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] +mode=trigger,level=warning:
+Warning: test error in PEAR.php on line 198
+[pear_error: message="test error" code=-42 mode=trigger level=warning prefix="" prepend="" append=""] +mode=trigger,level=error:
+Fatal error: test error in PEAR.php on line 198
diff --git a/pear/tests/pear_error2.phpt b/pear/tests/pear_error2.phpt new file mode 100644 index 0000000000..8d2531380e --- /dev/null +++ b/pear/tests/pear_error2.phpt @@ -0,0 +1,24 @@ +--TEST-- +PEAR_Error in die mode +--SKIPIF-- +--FILE-- +toString() . "\n"; + +?> +--GET-- +--POST-- +--EXPECT-- +mode=die: test error!! diff --git a/pear/tests/pear_error_callback.phpt b/pear/tests/pear_error_callback.phpt new file mode 100644 index 0000000000..4524c64607 --- /dev/null +++ b/pear/tests/pear_error_callback.phpt @@ -0,0 +1,32 @@ +--TEST-- +PEAR_Error in callback mode +--SKIPIF-- +--FILE-- +toString(); + print "\n"; +} +class myclass { + function error_method($obj) { + print "this is myclass::error_method reporting: "; + print $obj->toString(); + print "\n"; + } +} +$obj = new myclass; +new PEAR_Error("errortest1", 0, PEAR_ERROR_CALLBACK, "error_function"); +new PEAR_Error("errortest2", 0, PEAR_ERROR_CALLBACK, + array(&$obj, "error_method")); + + +?> +--GET-- +--POST-- +--EXPECT-- +this is error_function reporting: [pear_error: message="errortest1" code=0 mode=callback level=notice prefix="" prepend="" append=""] +this is myclass::error_method reporting: [pear_error: message="errortest2" code=0 mode=callback level=notice prefix="" prepend="" append=""]