RE: [PEAR-DEV] PEAR Error Management
| From: | Brent Cook | Date: | Tue, 24 Sep 2002 23:00:58 +0000 |
| Subject: | RE: [PEAR-DEV] PEAR Error Management | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-9462@lists.php.net to get a copy of this message | ||
On Tue, 24 Sep 2002, Steven Levin wrote:
> Hello All,
>
> I have been reading up on the Pear-Dev lists for a couple of weeks now
> but this is my first post. I would also like to see a tutorial for the
> PEAR_error class. I have been looking at some of the code in PEAR CVS
> and it seems as though pear has a standard way of doing it. First
> define all your error codes. Then extend PEAR_error and overide its
> constructor. Finally implement is_error and errorMessage which seem to
> be consistently similar among the developers. Is this true? I still
> feel like I am missing something. Any help would be greatly
> appreciated.
>
All I have ever done is to either inheirit PEAR or at least require
PEAR.php. Then, whenever I need to return an error, my classes just return
$this->raiseError('Message') or PEAR::raiseError('Message') depending on
the situation. It has worked well enough. Any code that uses such a class
just has to have this sort of structure to check for errors:
$result = $object->method();
if (PEAR::isError($result)) {
echo $result->getMessage();
exit;
}
If you have a method that uses the results of another method, just pass
along the error:
if (PEAR::isError($result)) {
return $result;
}
Why would you want to inheirit PEAR just to do error handling? If you need
it just to pass simple messages, you dont; just use the static
PEAR::raiseError(). If you want to do something extra to your error
messages, inheirit and override raiseError. For instance, the SQL_Parser
class overrides raiseError to draw an arrow pointing to where a syntax
error occurs. Other classes just add the class name, making debugging
easier:
function raiseError($message) {
return 'MyClass: '.$message;
}
That's all of the functionality I've ever used from PEAR's error system,
and it seems good enough so far. I get the feeling that ZEND 2's native
error handling will probably become the thing to use in the future, so
PEAR's error system hasn't gotten much better. It's fine for what I use
it for.
- Brent