/    Sign up×
Community /Pin to ProfileBookmark

PHP error handler not triggering soon enough

Hello.

I am trying to setup a custom error handler and it works, but it only is triggered when the error occurs and lets everything else run. For example, if the end of a script triggers an error, the top of the script is still executed. I would like the error handler to terminate the script when there is any error, no matter where it is. I also would like the script to be triggered when there is a FATAL_ERROR because right now a fatal error won’t trigger it. Here is the customer error handler function.

Thanks

[code=php]
//error handler function
function customError($errno, $errstr)
{
echo “<div class=’run_error’>Sorry, an error has occurred</div>”;
error_log(“Error: [$errno] $errstr”,1,
“myemail”,”From: myemail”);
die();
}

//set error handler
set_error_handler(“customError”);
[/code]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@bubakFeb 12.2010 — Hmm, that does not make sense. die is the equivalent to exit so that should work. I dont know, try exit; or exit("<div class='run_error'>Sorry, an error has occurred</div>");.

As for the type of error you need to handle that yourself. this example is pulled right out of the php bible, http://uk2.php.net/manual/en/function.set-error-handler.php

[code=php]
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />n";
echo "Aborting...<br />n";
exit(1);
break;

case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />n";
break;

case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />n";
break;

default:
echo "Unknown error type: [$errno] $errstr<br />n";
break;
}

/* Don't execute PHP internal error handler */
return true;
}
[/code]
Copy linkTweet thisAlerts:
@criterion9Feb 12.2010 — 
if the end of a script triggers an error, the top of the script is still executed
[/quote]

This is the expected behavior. The only way to prevent output from being sent to the browser (if that is what you are wanting) is to use buffered output such as ob_start(), etc and clearing the buffer in case of an error.


I also would like the script to be triggered when there is a FATAL_ERROR
[/quote]

As far as I know FATAL_ERRORs cannot trigger a custom error handler because errors that throw that error type indicate a failure in the processing engine.


Also from the page linked to in the previous post:

It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_types unless the callback function returns FALSE. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.

Also note that it is your responsibility to die() if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error.

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.
[/quote]
Copy linkTweet thisAlerts:
@bp_travisauthorFeb 12.2010 — I have found out that the script will execute HTML (HTML code not within <?php ?> tags) but will then die() when it gets to PHP code that has an error in it.
Copy linkTweet thisAlerts:
@criterion9Feb 12.2010 — The only way to prevent any output from being displayed if an error occurs is to use output buffering like I said previously.
Copy linkTweet thisAlerts:
@bp_travisauthorFeb 12.2010 — I have found out that the script will execute HTML (HTML code not within <?php ?> tags) but will then die() when it gets to PHP code that has an error in it.
Copy linkTweet thisAlerts:
@criterion9Feb 12.2010 — I have found out that the script will execute HTML (HTML code not within <?php ?> tags) but will then die() when it gets to PHP code that has an error in it.[/QUOTE]

Again...output buffering is the only way to keep that from happening (even when the html is outside <?php ?> tags).
Copy linkTweet thisAlerts:
@NogDogFeb 13.2010 — Again...output buffering is the only way to keep that from happening (even when the html is outside <?php ?> tags).[/QUOTE]

Which means the the ob_start() must be at the top of the file before any HTML output regardless of whether or not it's within <?php tags.
[code=php]
<?php
ob_start();
?>
<html>
<head>
[/code]
Copy linkTweet thisAlerts:
@toicontienFeb 15.2010 — Instead of using die(), try throwing and catching exceptions:
[code=php]if ( empty( $someVar ) ) {
throw new Exception( 'Could not find $someVar' );
}[/code]


Then elsewhere in your code, you can wrap a function that throws an exception in a try-catch block:
[code=php]try {
someFunction( $foo );
}
catch ( Exception $err ) {
myCustomErrorHandler( $err );
}[/code]
Copy linkTweet thisAlerts:
@bp_travisauthorFeb 26.2010 — Thanks for all the replies! I haven't had time to try them out yet, but I'll let you know when I do. Another question: can I make exceptions to an error handler? So if I want the handler to ignore a certain piece of the code, how would I do that?

Thanks
×

Success!

Help @bp_travis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...