Re: error handling

From: Date: Tue, 30 Jan 2001 16:33:33 +0000
Subject: Re: error handling
References: 1  Groups: php.pear 
Request: Send a blank email to php-pear+get-1225@lists.php.net to get a copy of this message
On Tue, 30 Jan 2001, Colin Viebrock wrote: | Okay, this may be confusing, so bear with me. | | I have my own error class which is an extention of PEAR_Error. | | All I need to pass my class is an error code, and the class handles | all the other stuff. Namely, depending on the code, I want the error | to either be a notice, a warning or a fatal error. So I have | something like: | | class myClass extends PEAR_Error { | | ... | | function myClass($code) { | $mode = PEAR_ERROR_RETURN; | $level = E_USER_NOTICE; | | if (in_array($code, $this->warn_errors)) { | $mode = PEAR_ERROR_TRIGGER; | $level = E_USER_WARNING; | } else if (in_array($code, $this->fatal_errors)) { | $mode = PEAR_ERROR_TRIGGER; | $level = E_USER_ERROR; | } | | return $this->PEAR_Error ('Some error', $code, $mode, $level ); | } | } | | | My problem is that warnings and fatal errors say: | | <br> | <b>Fatal error</b>: Some error in <b>PEAR.php</b> on line <b>206</b><br> | | Is it beyond the scope of PEAR to actually report the *real* script name and | line number on which the error occured? Right now it is. The bad part about using trigger_error() is of course that the reported line number will become that of the trigger_error call. If this is a problem for you, consider using a callback rather than a trigger: class myClass extends PEAR_Error { function myClass($code) { $mode = PEAR_ERROR_CALLBACK; $callback = "default_error_callback"; if (in_array($code, $this->warn_errors)) { $callback = "error_callback"; } else if (in_array($code, $this->fatal_errors)) { $callback = "fatal_error_callback"; } $this->PEAR_Error('Some error', $code, $mode, $callback) } } (Your example returned a PEAR_Error object from its constructor rather than calling the inherited constructor, I've shown "the intended way" in the example above.) It is possible to pass the actual file and line number around with __FILE__ and __LINE__, but that would actually require the user (programmer) to include these in their error invocations, which I personally find way too awkward to promote as a standard method. I'd rather see a "caller peek" function in Zend/PHP (AFAIR think Zend stores the context of the calling function, but not a full call stack). If we had a caller peek function PEAR::raiseError could use it to get the actual file/line. - Stig

« previous php.pear (#1225) next »