Re: Error handling and Error raising in PEAR

From: Date: Tue, 12 Aug 2003 21:14:05 +0000
Subject: Re: Error handling and Error raising in PEAR
References: 1 2 3 4 5 6 7 8 9  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-19603@lists.php.net to get a copy of this message
Hi, This is a very good point. If PEAR_Error were in its own file, and PEAR included that file, it would maintain BC, but also allow error-handling to be included without the other stuff. Pierre: I'm not positive I understand "Using always our own extended error class is not always a good thing" - do you mean to say that there are many cases where using an object to contain an error is not a good thing, and returning a boolean or null would work just as well? If so, I agree. There are some situations where errors shouldn't be returned at all, but a warning would be useful. Perfect example: there are a few packages that mistakenly use isError() instead of raiseError(). isError() could check its input, and if it isn't an object, raise a warning for callbacks to catch. Otherwise, a bug like this is extremely difficult to find, as it will almost never be noticed that an error occurred. I discovered this example because I was searching through CVS to see if any packages use the alternate isError() syntax PEAR::isError($error, ERROR_CODE) or PEAR::isError($error, 'message'). None of them do. It would improve performance to eliminate the code that handles this. Since isError() is the most commonly used PEAR function, it may improve performance noticeably in packages that use it alot. How's this look: /**
    * Tell whether a value is a PEAR error.
    *
    * @param   object $data   the value to test
    * @access  public
    * @return  bool    true if parameter is an error
    */
function isError($data) {
       if (is_object($data) && (get_class($data) == 'pear_error' ||
                                is_subclass_of($data, 'pear_error'))) {
            return true;
       } else {
            // perhaps use trigger_error with debug_backtrace() and E_USER_WARNING
            $a = debug_backtrace();
            trigger_error('PEAR WARNING: ' . gettype($data) . ' passed to PEAR::isError()'
               . ' in '.$a[0]['file'].' line '.$a[0]['line'], E_USER_WARNING);
       }
       return false;
} This code would be faster, and also validate input better. Greg Pierre-Alain Joye wrote:
On Tue, 12 Aug 2003 22:18:56 +0200 Alexander Merz <alexander.merz@web.de> wrote:
Greg Beaver wrote:
class PEAR extends PEAR_Error
     
hm, a really intresting starting point I think i need some days to realize the consequences, it is the first new point of view a side to PEAR_Error2Ext...
Little sidenote. In many cases, we do not need even to include the PEAR_Error and/or PEAR by default. But only when required. Using always our own extended error class is not always a good thing. pierre


« previous php.pear.dev (#19603) next »