473,219 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

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
Jul 17 '05 #1
5 2019
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?

Jul 17 '05 #2
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)
Jul 17 '05 #3

"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:11*********************@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.
Jul 17 '05 #4
"steve" <Us************@dbForumz.com> wrote in message
news:4_***************************************@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().
Jul 17 '05 #5
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/

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
11
by: varois83 | last post by:
Hi I am in the process of creating a guestbook for my site, I am a newbie and used several tutorials and customized them to what I need using the little knowledge I got. I get the following...
3
by: Mohammad S Khan | last post by:
I am currently doing some research on reporting features provided / compatible with dotnet features. Our current application has excel reprots that are emailed both with an without scheduling...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
3
by: jez123456 | last post by:
Hi My users, use ms access for querying and reporting on a backend database, however they need to replace ms access with a new reporting tool. I’m thinking of developing my own reporting...
6
by: John Lau | last post by:
Hi, I am looking at the MS KB Article 306355: HOW TO: Create Custom Error Reporting Pages in ASP.NET by Using Visual C# .NET This article describes how to redirect errors to a custom html...
4
by: Sandy | last post by:
Hello - I read an interesting article on the web wherein the author states he doesn't handle too many errors at page level, but handles them at the application level. He further goes on to show...
0
by: Joshua V. | last post by:
We are trying to implement Windows Error Reporting from within our VB.Net application. Basically we want to replace the standard unhandled exception message we use with the Windows Error Reporting...
13
by: Dave Rado | last post by:
Hi The W3C css validation service at http://jigsaw.w3.org/css-validator/ claims that: {font-family: "Book Antiqua", Palatino, "Times New Roman", "Times Roman" serif} is invalid on the...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.