Elegant error reporting, possible?

This is a discussion on Elegant error reporting, possible? within the PHP Language forums, part of the PHP Programming Forums category; Hi, In my script (phpnuke), whenever there is access to database, there is this line of code: message_die(GENERAL_ERROR, ’some ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-14-2005
steve
 
Posts: n/a
Default 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?

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Elegant-...ict206147.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=703259
Reply With Quote
  #2 (permalink)  
Old 03-14-2005
R. Rajesh Jeba Anbiah
 
Posts: n/a
Default [FAQ] How to handle the script or database errors elegantly? (Was Re: Elegant error reporting, possible?)

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?

Reply With Quote
  #3 (permalink)  
Old 03-14-2005
Kenneth Downs
 
Posts: n/a
Default Re: Elegant error reporting, possible?

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)
Reply With Quote
  #4 (permalink)  
Old 03-14-2005
Chung Leong
 
Posts: n/a
Default Re: [FAQ] How to handle the script or database errors elegantly? (Was Re: Elegant error reporting, possible?)


"R. Rajesh Jeba Anbiah" <ng4rrjanbiah@rediffmail.com> wrote in message
news:1110771493.119321.58090@g14g2000cwa.googlegro ups.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.


Reply With Quote
  #5 (permalink)  
Old 03-14-2005
Chung Leong
 
Posts: n/a
Default Re: Elegant error reporting, possible?

"steve" <UseLinkToEmail@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().


Reply With Quote
  #6 (permalink)  
Old 03-15-2005
ng4rrjanbiah@rediffmail.com
 
Posts: n/a
Default Re: [FAQ] How to handle the script or database errors elegantly? (Was Re: Elegant error reporting, possible?)

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: http://rajeshanbiah.blogspot.com/

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:09 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0