Page 1 of 1
Error handling in development and production
Posted: Thu Jan 04, 2007 11:37 pm
by maxd
I am pretty much a newbie to PHP. I've built several PHP/MySQL sites, simple stuff, CMS, etc. Nothing revolutionary. I am a bit more experienced with Coldfusion. In CF, I build in error handling in the Application.cfc file, which runs every time a request is made. I can choose to display errors on screen (which I do in development) and then easily switch to a log/redirect system once I publish the site. This way, if a request does generate an error, the user is gracefully redirected to a custom page, letting them know that their request created an error, and that they can contact us if they continue to experience problems.
Is there a way to do something similar in PHP? So far, I've just used
in development, and have turned off error reporting and hoped for the best in production

VERY BAD PRACTICE.
Does anyone have some insights into how to create a good error handling system for development, then production which creates an opaque user experience on the production side, while still logging errors and emailing/tracking in DB errors that do get generated?
Thanks,
max
Posted: Fri Jan 05, 2007 9:06 am
by feyd
I typically leave error_reporting at E_ALL in production, but I turn off display_errors and make sure error_log is set properly. However I also use my own error handling system too, but it cannot catch all errors. Anything in the page level is swallowed, handled internally and as gracefully as I can manage give the error.
Re: Error handling in development and production
Posted: Fri Jan 05, 2007 9:15 am
by onion2k
maxd wrote:and have turned off error reporting and hoped for the best in production

VERY BAD PRACTICE.
Very good practise actually. Users should never, ever see a code error. It's much better for a page to die quietly that for a user to see what went wrong. Better still is to have something catch the error and die gracefully while notifying you, but that's not always possible (like feyd says).
Posted: Fri Jan 05, 2007 10:41 am
by maxd
Thanks for your replies. Being a neophyte, it really makes me feel better having input from experienced developers like yourselves. I don't think I've ever had a response to one of my posts from feyd, but have read so many of feyd's erudite replies throughout the forum, I'm honored!
Sounds like my method is not far off of accepted practice after all, which is a relief.
I would love to figure out a way to redirect users to a "error page" which informs them - without detail - that their request failed in some way. I'll explore this further. Is it as simple as placing a "location" tag in the error handler? I seem to run into "header already sent" problems whenever I start messing with header("location...").
The only other question would be regarding logging. As onion2k and feyd both mentioned, configuring error_log properly is best, but what about in a shared server environment? Would you recommend creating an error log file in the web directory? Or somehow log in a database?
Again, I'll explore these options myself, but any insight from experienced developers is more than welcome!
Posted: Fri Jan 05, 2007 10:47 am
by Begby
If you are using PHP 5 you can use exceptions to do what you want. You can create a debug mode constant and based on that either display the exception when it occurs, or log it and show a friendly error message.
Posted: Fri Jan 05, 2007 10:55 am
by feyd
In shared environments the error_log is correctly set to go to syslogd or system log file. This information is often accessible via your control panel. Unless you can point the setting to a non web accessible location I wouldn't set it to write into your own account space.
As for the
header() problems, process all page logic before displaying anything. That's one of the better ways to get around them. Secondly, use
headers_sent() so you don't even attempt to output a header if the headers have been sent.
Posted: Fri Jan 05, 2007 11:40 am
by aaronhall
set_error_handler would allow you to log to the database (if there isn't a database connection error) and forward the user to a different page (if headers haven't been sent, as feyd mentioned).
At worst, you'd be able to print a user-friendly error message if the script dies.
EDIT: note that this will completely override PHP's inbuilt error handling, so you won't find a record of it in apache's error logs.
Posted: Fri Jan 05, 2007 1:48 pm
by xpgeek
i am
set error reporting to E_ALL
write my own error handler where i send all errors, warnings and notice in my email,
then something wrong i quikly fix it.