473,401 Members | 2,146 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,401 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 2024
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.