Home > PHP >
Debug PEAR Based Scripts Easier | Sitemap Search |
|
Sections Membership Features
Recent comments
very difficult by alfin Taking the credit for another persons work ? by curious dude. |
Debug PEAR Based Scripts EasierPosted by martin on 1 Jun 2002. Error handling in PEAR can be used to make error messages on your development machine more verbose, while keeping them short for your visitors without much hassle. When you develop some code on your machine you usually want all information on the errors you get. Contrary to that you don't want to reveal all this info to the everyday website visitor. Besides the fact that they usually won't understand it there are people who just wait to see something like that and get inside information that may make your site vulnerable to attacks. The usual way to do itYou can How PEAR changed thingsPEAR code has a simple yet very useful way to pass errors - through error objects that are instantiated when something fails. If you have looked at the example code from the PEAR manual you've already seen how to use that when a database query fails for example. The example shows:
if (DB::isError($result)) {
die ($result->getMessage());
}
The Surely this can help but usually you'll want to see the query which caused the error,
the old way to do that was to start looking in the sources until
you find the piece of code which cause the error and then
What else can be displayedWhen the
The developer info contains the failed query along with the error message from the database server. Example: SLECT * FROM mytable [nativecode=1064 ** You have an error in your SQL syntax near 'SLECT * FROM mytable' at line 1] This info can be fetched by the How do I automate thisYou are already displaying messages with the To accomplish this you need to modify the
function getMessage()
{
return ($this->error_message_prefix . $this->message);
}
This can be found in the
function getMessage()
{
return ($this->error_message_prefix . $this->message .
'<br />' . $this->userinfo);
}
The next time a query fails you will see it displayed along with the almost useless default message. It can be even easierWith the PEAR error handling you can even get rid of all that manual checks
whether the query failed by setting an appropriate error mode. The default mode
called The mode can be
function handlePearError($error) {
echo "$error->message<br />\n",
$error->userinfo;
exit;
}
If you want to use it on a public server just remove the line which displays
If you need to temporarily set the error handling for an object you can do
Commentsthanks for (push|pop)ErrorHandling by David Mintz (dmintz@davidmintz.org) on 12 Apr 2004 6:04pm GMT thanks for the tip about pushErrorHandling() and popErrorHandling(). These handy little tools don't seem to be documented at http://pear.php.net/ PEAR Error Insights by Jim Tom Polk (datdone@camalott.com) on 27 Nov 2004 8:36am GMT Thanks for writing up what you have learned about PEAR error handling. The PEAR site makes quite an ado about this feature, yet lacks concrete examples that provide real world benifits. After I found how good the feedback is that can be obtained, while trying to figure out what was wrong with a QuickForm to DB's autoexecute(), in seconds I had solved the programming problem. Then I went in search of information. That is when I found you site and some extra nuggets of wisdom to tuck away. Thanks... Jim Tom Polk |