Re: PEAR::expectError
| From: | (Stig Sæther Bakken) | Date: | Tue, 14 Aug 2001 18:52:42 +0000 |
| Subject: | Re: PEAR::expectError | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-1482@lists.php.net to get a copy of this message | ||
["Tomas V.V.Cox" <cox@idecnet.com>]
> Stig Sæther Bakken wrote:
> >
> > Hi,
> >
> > Have you ever had an error callback function, but you needed to check
> > if a DB query returned just a specific error?
>
> nextID for example? :)
>
> > Here's a suggestion:
> >
> > $dbh->expectError(DB_ERROR_SYNTAX);
> > $err = $dbh->query("BAD QUERY");
> > if (DB::isError($err) && $err->getCode() == DB_ERROR_SYNTAX) {
> > print "it works!";
> > }
>
> Umm... sorry but I don't undestand that very well. expectError()
> disables the actual error handling, ok, but what is supposed to be the
> pourpose of the error code you give to the function?
> In your example I don't see also when you restore the error handler, may
> be before raise the error? Isn't that equivalent to pop/push error
> handlers?
No, because it will only change the error mode for _specific_ errors.
Others will go straight through and run a callback or whatever.
For example, you want to do an INSERT, but you don't want your page to
bomb out just because the primary key already existed. So you do
expectError(DB_ERROR_ALREADY_EXISTS) before the query, and
expectError(null) after (to "un-expect").
> > Check out the PEAR class from CVS to test it. I have told myself to
> > not start implementing much more error-handling magic until Zend 2.0
> > appears, but this one wanted out very badly. The thing that's missing
> > from it now is stacking, so you can push and pop expected errors.
>
> Perhaps we could simplify things (at least pop/push) and do all only
> with setErrorHandling. For example:
>
> // push a new mode
> $db->setErrorHandling($mode);
> $err = $db->query();
> // pop if no args
> $db->setErrorHandling();
This would not be the same thing, since it doesn't give you the extra
resolution that expectError does. If you think Java exceptions, your
example is:
try {
} catch (Exception e) {
}
This one catches all or nothing. The simplest use of expectError will
be:
try {
} catch (DBAlreadyExistsException e) {
}
Which catches that specific error, but "re-throws" any other errors.
- Stig
--
Stig Sæther Bakken <ssb@alltheweb.com>
Fast Search & Transfer ASA, Trondheim, Norway