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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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? |
|
|||
|
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) |
|
|||
|
"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. |
|
|||
|
"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(). |
|
|||
|
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/ |
![]() |
| Thread Tools | |
| Display Modes | |
|
|