diff options
author | Stig Bakken <ssb@php.net> | 2000-09-09 02:39:56 +0000 |
---|---|---|
committer | Stig Bakken <ssb@php.net> | 2000-09-09 02:39:56 +0000 |
commit | 971f8b08811b1dfef47841dd63c3ce349959e64f (patch) | |
tree | d363b6381f6a059fc8a8d5defceec17a414ad25c | |
parent | 7918735b11cf38134e422daf162086ba81376204 (diff) | |
download | php-git-971f8b08811b1dfef47841dd63c3ce349959e64f.tar.gz |
@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.
#
-rw-r--r-- | pear/PEAR.php.in | 59 | ||||
-rw-r--r-- | pear/tests/DB_Error.phpt | 77 | ||||
-rw-r--r-- | pear/tests/DB_factory.phpt | 46 | ||||
-rw-r--r-- | pear/tests/DB_parseDSN.phpt | 38 | ||||
-rw-r--r-- | pear/tests/pear1.phpt | 39 | ||||
-rw-r--r-- | pear/tests/pear_error.phpt | 69 | ||||
-rw-r--r-- | pear/tests/pear_error2.phpt | 24 | ||||
-rw-r--r-- | pear/tests/pear_error_callback.phpt | 32 |
8 files changed, 215 insertions, 169 deletions
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,6 +200,18 @@ 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; @@ -191,6 +219,20 @@ class PEAR_Error } // }}} + // {{{ 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-- -<?php if (!include("DB.php")) print "skip"; ?> ---FILE-- -<?php // -*- C++ -*- - -// Test for: DB.php -// Parts tested: DB_Error, DB_Warning - -require_once "DB.php"; - -/* -print "testing DB::parseDSN...\n"; -var_dump(DB::parseDSN("mysql")); -var_dump(DB::parseDSN("odbc(mssql)")); -var_dump(DB::parseDSN("mysql://localhost")); -var_dump(DB::parseDSN("mysql://remote.host.com/db")); -var_dump(DB::parseDSN("mysql://testuser:testpw")); -var_dump(DB::parseDSN("oci8://user:pass@tns-name")); -var_dump(DB::parseDSN("")); -var_dump(DB::parseDSN("")); -var_dump(DB::parseDSN("")); -var_dump(DB::parseDSN("")); -var_dump(DB::parseDSN("")); -var_dump(DB::parseDSN("")); -var_dump(DB::parseDSN("")); -*/ - - -print "testing different error codes...\n"; -$e = new DB_Error(); print $e->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=""] -<br> -<b>Notice</b>: DB Error: syntax error in <b>PEAR.php</b> on line <b>182</b><br> -testing different error serverities... -<br> -<b>Notice</b>: DB Error: syntax error in <b>PEAR.php</b> on line <b>182</b><br> -<br> -<b>Warning</b>: DB Error: syntax error in <b>PEAR.php</b> on line <b>182</b><br> -<br> -<b>Fatal error</b>: DB Error: syntax error in <b>PEAR.php</b> on line <b>182</b><br> 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-- -<?php if (!include("DB.php")) print "skip"; ?> ---FILE-- -<?php // -*- C++ -*- - -// Test for: DB.php -// Parts tested: DB_Error, DB_Warning - -require_once "DB.php"; - -$backends = array( - "ibase", - "msql", - "mssql", - "mysql", - "oci8", - "odbc", - "pgsql", - "sybase" -); - -reset($backends); -while (list($i, $name) = each($backends)) { - print "testing $name: "; - $obj = DB::factory($name); - if (DB::isError($obj)) { - print "error: ".$obj->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-- -<?php if (!include("DB.php")) print "skip"; ?> ---FILE-- -<?php // -*- C++ -*- - -// Test for: DB.php -// Parts tested: DB_Error, DB_Warning - -require_once "DB.php"; - -function test($dsn) { - print implode(",", DB::parseDSN($dsn))."\n"; -} - -print "testing DB::parseDSN...\n"; -test("mysql"); -test("odbc(mssql)"); -test("mysql://localhost"); -test("mysql://remote.host.com/db"); -test("mysql://testuser:testpw"); -test("oci8://user:pass@tns-name"); -test("odbc(solid)://foo:bar@tcp+localhost+1313"); - -/* phptype,dbsyntax,protocol,hostspec,database,username,password */ -?> ---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-- +<?php + +require_once "PEAR.php"; + +class TestPEAR extends PEAR { + function TestPEAR($name) { + $this->_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-- +<?php // -*- C++ -*- + +// Test for: PEAR.php +// Parts tested: - PEAR_Error class +// - PEAR::isError static method +// testing PEAR_Error + +require_once "PEAR.php"; + +error_reporting(4095); + +print "default PEAR_Error: "; +$err = new PEAR_Error; +print $err->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: <br> +<b>Notice</b>: test error in <b>PEAR.php</b> on line <b>198</b><br> +[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] +mode=trigger,level=notice: <br> +<b>Notice</b>: test error in <b>PEAR.php</b> on line <b>198</b><br> +[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] +mode=trigger,level=warning: <br> +<b>Warning</b>: test error in <b>PEAR.php</b> on line <b>198</b><br> +[pear_error: message="test error" code=-42 mode=trigger level=warning prefix="" prepend="" append=""] +mode=trigger,level=error: <br> +<b>Fatal error</b>: test error in <b>PEAR.php</b> on line <b>198</b><br> 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-- +<?php // -*- C++ -*- + +// Test for: PEAR.php +// Parts tested: - PEAR_Error class +// - PEAR::isError static method +// testing PEAR_Error + +require_once "PEAR.php"; + +error_reporting(4095); + +print "mode=die: "; +$err = new PEAR_Error("test error!!\n", -42, PEAR_ERROR_DIE); +print $err->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-- +<?php + +require_once "PEAR.php"; + +function error_function($obj) { + print "this is error_function reporting: "; + print $obj->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=""] |