Re: Re: Error recovery - fatal errors

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 14 May 2011 at 15:05, Peter Lind <peter.e.lind@xxxxxxxxx> wrote: 

> On 14 May 2011 12:33, Tim Streater <tim@xxxxxxxxxxxxxxxx> wrote:
>> I would like, in my app, to recover from as many run-time errors as possible,
>> so that I can tidy up. And unsolicited output generated by the standard error
>> system is really unhelpful as it becomes part of the ajax reply to the
>> browser.
>>
>> So I've added my own error handler, but it seems that I can't catch fatal
>> errors. The error in question comes from doing something like:
>
> Fatal errors are fatal - if you could recover from them, they wouldn't be
> fatal.

Except that this error is arbitrarily designated as fatal when it need not be. A few days ago I discovered register_shutdown_function as mentioned by someone today, and use that to pass E_ERRORs on to my error handler (as declared by set_error_handler). That way I can log the error properly and notify the user in a consistent manner. I've tested this by introducing some errors (e.g. unitialised variables or setting $dbh to null) and these are all nicely picked up.

>> $res = $dbh->query ($sql);
>>
>> with $sql being an SQL statement, and $dbh being a database handle. I
>> recently had a case where $dbh was NULL, which triggers a fatal error from
>> SQLite. In principle such a bug should show up quickly, but this one had lain
>> untriggered for about a year. It seems to me somewhat arbitrary for this to
>> be designated a fatal error. Is there a way I can catch these? Most SQLite
>> error situations I'm solving with try/catch but no luck with this one so far.
>
> You've got something wrong: either $dbh is not null or the error is
> not from sqlite. I'm guessing the former. To avoid situations like
> that, do proper error checking (i.e. actually check that your database
> connection was opened succesfully).

No, $dbh was unitialised. It was a coding error where I was using $dbhs instead of $dbh. Since what I was apparently trying to do last year was wrong anyway I've rejigged that section. And obviously I do check that the db is opened; just that in this instance I was using the wrong variable.

>> Error handling in library packages seems somewhat arbitrary - e.g. opendir
>> may give an E_WARNING, but closedir, readdir don't.
>
> You can avoid all problems with error output by turning off error
> displays in php.ini (set display_errors = off) - use error logging
> instead. That's the recommended setting for production servers.

This is not a browser/webserver situation in the classic manner. In this case, the browser, PHP code, and the instance of apache used are all running on the user's machine. The user just thinks they are running a local application.

Cheers  --  tim


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux