Welcome to dbForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Elegant error reporting, possible?

 
   Database Forums (Home) -> PHP RSS
Next:  Server not found  
Author Message
steve




Joined: Jan 06, 2004
Posts: 655



(Msg. 1) Posted: Sun Mar 13, 2005 9:10 pm
Post subject: Elegant error reporting, possible?

Hi,
In my script (phpnuke), whenever there is access to database, there is this line of code:

message_die(GENERAL_ERROR, 'some error msg', '', __LINE__, __FILE__, $sql);

Is there a more elegant way of reporting line number besides putting this line everywhere I access db. I want to just write a function, which also globally knows about the current line number(?) and in case of error reports it.

Is there any way to do that?

 >> Stay informed about: Elegant error reporting, possible? 
Back to top
Login to vote
R. Rajesh Jeba Anb1

External


Since: Jun 15, 2004
Posts: 63



(Msg. 2) Posted: Sun Mar 13, 2005 9:10 pm
Post subject: [FAQ] How to handle the script or database errors elegantly? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Q: How to handle the script or database errors elegantly?
A: You can route the errors to a handler/your custom function using
set_error_handler() function. However, you may not capture all errors.

Refer:
http://www.php.net/set_error_handler
http://www.phpclasses.org/ErrorHandler
http://www.php.net/trigger_error

Q: I want to capture fatal errors too. But, set_error_handler() doesn't
capture. Is it a bug?
A: It's a defined behavior. You may use output buffering techniques to
capture fatal errors. That is, you capture the whole output with a
output buffering callback function and then parse the content to find
out the fatal errors.

Refer:
http://www.php.net/set_error_handler#35622
http://www.php.net/ob_start


+++++
@todo Grammar fix. Better hack?

 >> Stay informed about: Elegant error reporting, possible? 
Back to top
Login to vote
Kenneth Downs

External


Since: Mar 05, 2005
Posts: 230



(Msg. 3) Posted: Mon Mar 14, 2005 12:00 am
Post subject: Re: Elegant error reporting, possible? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

steve wrote:

 > Hi,
 > In my script (phpnuke), whenever there is access to database, there is
 > this line of code:
 >
 > message_die(GENERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__,
 > $sql);
 >
 > Is there a more elegant way of reporting line number besides putting
 > this line everywhere I access db. I want to just write a function,
 > which also globally knows about the current line number(?) and in case
 > of error reports it.
 >
 > Is there any way to do that?
 >

One typical way is to first off wrap all of your data access commands in a
single function, this is mine, which hits Postgres:

function SQLExec($sql_command) {
global $dbconn
$errlevel = error_reporting(0);
pg_send_query($dbconn,$sql);
$results=pg_get_result($dbconn);
$t=pg_result_error($results);
if ($t) {
ErrorAdd($t);
ErrorAdd("Command was: $sql");
}
error_reporting($errlevel);
return $results;
}

Another advantage of this is that you are a step closer to platform
independence on the db side.

But for error handling you put this at the top of your dispatcher:

$GLOBALS["errors"]=array();

Now you toss this function at the bottom of the dispatcher. I don't do line
numbers myself in PHP, but if you want them it would be something like
this:

function ErrorAdd($string,$line,$file) {
$GLOBALS["errors"][] =
"Error in $file at line $line: $string";
}

Now give yourself this function:

function Errors() { return count($GLOBALS["errors"]>0; }

which allows to control execution based on existence of prior errors. Some
code maybe needs to run no matter what, and some should not run if there
have been earlier errors.

The icing on the cake is:

function ErrorsHTML() {
if (!Errors()) { return ""; }
$HTML_errs="";
foreach($GLOBALS["error"] as $err) {
$HTML_errs.=$err."<br>\n";
}
return '<div class="errors">'.$HTML_errs.'</div>';
}

so you can now put the following unconditional code at the top of each page,
or in your dispatcher:

<?php echo ErrorsHTML(); ?>

....then you move on to other things in life.

Hope this helps.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Elegant error reporting, possible? 
Back to top
Login to vote
Chung Leong

External


Since: Dec 06, 2003
Posts: 245



(Msg. 4) Posted: Mon Mar 14, 2005 1:40 pm
Post subject: Re: [FAQ] How to handle the script or database errors elegan [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"R. Rajesh Jeba Anbiah" <ng4rrjanbiah.DeleteThis@rediffmail.com> wrote in message
news:1110771493.119321.58090@g14g2000cwa.googlegroups.com...
 > Q: I want to capture fatal errors too. But, set_error_handler() doesn't
 > capture. Is it a bug?
 > A: It's a defined behavior. You may use output buffering techniques to
 > capture fatal errors. That is, you capture the whole output with a
 > output buffering callback function and then parse the content to find
 > out the fatal errors.

Are you sure that works? IIRC, a fatal error would cause the output buffer
to get flushed.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Elegant error reporting, possible? 
Back to top
Login to vote
Chung Leong

External


Since: Dec 06, 2003
Posts: 245



(Msg. 5) Posted: Mon Mar 14, 2005 1:40 pm
Post subject: Re: Elegant error reporting, possible? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"steve" <UseLinkToEmail.TakeThisOut@dbForumz.com> wrote in message
news:4_703259_1e8c2956b514a921086c731313b922e1@www.dbforumz.com...
 > Hi,
 > In my script (phpnuke), whenever there is access to database, there is
 > this line of code:
 >
 > message_die(GENERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__,
 > $sql);
 >
 > Is there a more elegant way of reporting line number besides putting
 > this line everywhere I access db. I want to just write a function,
 > which also globally knows about the current line number(?) and in case
 > of error reports it.
 >
 > Is there any way to do that?

Yes. Use debug_backtrace().<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Elegant error reporting, possible? 
Back to top
Login to vote
ng4rrjanbiah

External


Since: Mar 11, 2005
Posts: 1



(Msg. 6) Posted: Mon Mar 14, 2005 7:57 pm
Post subject: Re: [FAQ] How to handle the script or database errors elegan [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Chung Leong wrote:
  > > Q: I want to capture fatal errors too. But, set_error_handler()
doesn't
  > > capture. Is it a bug?
  > > A: It's a defined behavior. You may use output buffering techniques
to
  > > capture fatal errors. That is, you capture the whole output with a
  > > output buffering callback function and then parse the content to
find
  > > out the fatal errors.
 >
 > Are you sure that works? IIRC, a fatal error would cause the output
buffer
 > to get flushed.

I'm sure, it works. 'coz I have written and using a proprietary
class that does all these stuffs like displaying sources with
highlighted error line, etc; though it won't capture parser error.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: <a style='text-decoration: underline;' href="http://rajeshanbiah.blogspot.com/" target="_blank">http://rajeshanbiah.blogspot.com/</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: Elegant error reporting, possible? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
PHP Reporting in PHP Code? - Could someone recommend a PHP report generator which is written in PHP, can be used by non-programmers, save report templates. I have searched the web, but the only products I have found run under Windows. I can only run the report generator on the..

parse error or infinite loop? a blank screen, yet the php .. - Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString", $formName); // 06-22-07 - the next comm...

Error log. - Is there an error log in which PHP reports script related problems ? Peter

Error - I m getting this error while uploading an image through form copy(http://localhost/madame/uploads/alt/medium/zoom.gif) [function.copy]: failed to open stream: HTTP wrapper does not support writeable connections. in..

Parse error - Hi, What's wrong with this line? I'm getting parse errors for it: if ( empty($payment) || !is_numeric($payment) ) {
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]