Re: URGENT HELP on PEAR error-handling needed
| From: | Tomas V.V.Cox | Date: | Thu, 13 Dec 2001 10:51:05 +0000 |
| Subject: | Re: URGENT HELP on PEAR error-handling needed | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-601@lists.php.net to get a copy of this message | ||
Karsten Kraus wrote:
>
> Hi List,
>
> Now I need some help on using PEAR's error-handling system.
> Almost all of my classes inherit form one baseclass which has PEAR as
> parent-class.
> Those which don't inherit from that baseclass have a reference to it, so
> they can call
> methods that way: $this->_myBase->debug('message', level) (just an example).
>
> What I would like to have is one central error-handling-method (in my
> baseclass, i think)
> which knows/and get's all the errors. So that if I have to test a value, I
> use baseclass::isError() and get true if the value is an pear_error or
> inherited (like DB_Error) from it.
> On the other hand if there is an error the baseclass does some logging and
> other error-handling (showing the error-page, perhaps even rolling back
> db-writes (not by using db::rollback, but by just triggering methods in the
> class which raised the error)
If yout have something like this:
Foo extends Base extends PEAR
My recomendation is to ovewrite the "raiseError()" method with your own
one in the Base class.
For ex:
class Base extends PEAR
{
function raiseError($msg)
{
// roll back trans
// log the error
// more things you want to check
PEAR::raiseError($msg);
}
}
For throwing errors in class Foo you could do:
if ($error) {
return $this->raiseError($error);
// or also:
// return Base::raiseError($error);
}
In the code of your app:
<?php
require_once 'PEAR.php';
PEAR::setErrorHandling(PEAR_ERROR_DIE); // or the action you want
// when an error occurs
// or for example
$foo = new Foo;
$foo->setErrorHandling(PEAR_ERROR_DIE);
?>
Other mode is to set up a function that will handle all PEAR errors. For
example:
function pear_error($obj) {
// do something
die($obj->getMessage);
}
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'pear_error');
In http://pear.php.net/manual and http://vulcanonet.com/soft/pear_tut/
there are more examples of this.
> I admit, I didn't much 'googleing' on that and didn't have the time to read
> the source, so I don't really know if this already is the standard behavior
> of PEAR::iserror().
PEAR::isError() is the only method you need to check errors from PEAR
classes.
Tomas V.V.Cox