Error handling in development and production

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maxd
Forum Commoner
Posts: 41
Joined: Sun Dec 04, 2005 12:12 am
Location: Denver

Error handling in development and production

Post 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

Code: Select all

error_reporting(E_ALL);
in development, and have turned off error reporting and hoped for the best in production 8O 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Error handling in development and production

Post by onion2k »

maxd wrote:and have turned off error reporting and hoped for the best in production 8O 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).
maxd
Forum Commoner
Posts: 41
Joined: Sun Dec 04, 2005 12:12 am
Location: Denver

Post 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!
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post 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.
Post Reply