Jump to content

Decent Error Reporting


JasonLewis

Recommended Posts

I'm trying to create decent error reporting. Basically I run a query like this:

 

$query = $DB->query("SELECT `blah` FROM `table` WHERE `foo`='bar'", err());

 

Now the second parameter for that function is calling another function which looks like this:

 

function err(){
return array("file" => basename(__FILE__), "line" => __LINE__);
}

 

Which I thought would work. Because I want to say something along the lines of "Error was found in index.php on line 54".

 

But it will only return the line that the return is on. Which, I guess, makes sense. Rather annoying though.

Is there another way of going about this which doesn't go like this:

 

$query = $DB->query("SELECT `blah` FROM `table` WHERE `foo`='bar'", array("file" => basename(__FILE__), "line" => __LINE__));

 

Suggestions welcome.

Cheers.

Link to comment
Share on other sites

Okay cheers that's working okay. I've done it like this:

 

$query = $DB->query("SELECT `blah` FROM `table` WHERE `foo`='bar'", new Exception());

 

Then in my query function I'm just using $e->getLine() and $e->getFile().

 

Is that okay to do it like that?

Link to comment
Share on other sites

I'm not throwing the exception. I'm just making a new exception, then getting the line number and file name from it.

 

die("MySQL Query failed.<br />MySQL said: ".mysql_error()."<br />Error was found in ".($err->getFile())." on line ".($err->getLine()));

 

Where $err is the new Exception().

Link to comment
Share on other sites

Sorry for being blunt, but that' s just stupid. Never, ever, ever call die() or exit() on error. You don't care for an error log?

 

Just throw the exception. If you want, you can insert %s placeholders in the error message and use sprintf to insert the feedback from mysql_error().

Link to comment
Share on other sites

Why not use the try statement ?

 

<?php
$query = "SELECT * from TABLE WHERE yadayada = "yadayada";
try {
if (!$qResult = mysql_query($query)) {
	$mysql_error = mysql_error();
	throw new Exception("Error bla bla bla <br />Reason: $mysql_error");
	return false;
}
## Enter the code to run when all goes well here.....
}
catch (Exception $e) {
$error = $e->getMessage();
$script = $e->getFile();
log_error($error, $script);
echo "Error bla bla bla.";
}
?>

And then there's the function to log the error:

 

<?php
function Log_error($error, $script) {
$date = date("d-m-Y");
$time = date("H-i-s");
$error = mysql_real_escape_string($error);
$script = mysql_real_escape_string($script);
$insError = ("INSERT INTO error_log (date, time, error, source) VALUES('$date', '$time', '$error', '$script')");

try {
	if (!$qResult = mysql_query($insError)){
		$mysql_error = mysql_error();
		throw new Exception("Unable to update error log. <br />Reason: $mysql_error");
	}
}
catch (Exception $e) {
	echo $e->getMessage();
}
}
?>

You could also write to a text file or whatever...

This will log the error message, script generating the error and the time it occurred...

Link to comment
Share on other sites

Check out section 3.8 of my intro into OOP in PHP on the main site for a basic example of using exceptions. Note that you do NOT need to catch an exception that you intend to be fatal!

 

Why not use the try statement ?

See my comment above.

 

You could also write to a text file or whatever...

This will log the error message, script generating the error and the time it occurred...

If you don't want the error to be displayed, toggle the display_errors directive off. And use the error log to log errors. That's what it is for. Logging errors to a database is unwise.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.