I am trying to create a custom error function (following a tutorial). I have:
<?php
error_reporting(E_ERROR);
function handleError ($errno, $errstr,$error_file,$error_line){
echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line";
echo "<br />";
echo "Terminating PHP Script";
die();
}
//set the error handler here, override the default
set_error_handler("handleError");
//cause a crash
myfunction();
?>
However my script isn't calling the function. It just prints the default error message. Could someone give me a pointer to what I might be doing wrong here please? Is my error_reporting value wrong?
There is nothing wrong with your code. However, the capabilities of set_error_handler are limited.
From the PHP documentation:
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.
If you indeed need to catch compilation errors as in your example, there is one workaround mentioned in the comments of the documentation above - the use of a shutdown function:
<?php
error_reporting(E_ERROR);
function handleError($errno, $errstr, $error_file, $error_line) {
echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line";
echo "<br />";
echo "Terminating PHP Script";
die();
}
function checkForError() {
$error = error_get_last();
if ($error["type"] == E_ERROR) {
handleError($error["type"], $error["message"], $error["file"], $error["line"]);
}
}
register_shutdown_function("checkForError");
// cause a crash
myfunction();
?>
Note that the default error handler will still be called, so this prints out:
Fatal error: Call to undefined function myfunction() in path\to\file.php on line 24
Error: [1] Call to undefined function myfunction() - path\to\file.php:24
Terminating PHP Script
You can get rid of the default message by disabling error reporting with error_reporting(0);.
If you want to handle errors from within your method execution (i.e. you have defined myfunction somewhere), your original example might already work, depending on your concrete case. Proof, e.g.:
<?php
error_reporting(E_ERROR);
function handleError($errno, $errstr, $error_file, $error_line) {
echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line";
echo "<br />";
echo "Terminating PHP Script";
die();
}
function myfunction() {
fopen("nonexistingfile", "r");
}
// set the error handler here, override the default
set_error_handler("handleError");
// cause a crash
myfunction();
?>
This uses the custom error handler as expected and prints out:
Error: [2] fopen(nonexistingfile): failed to open stream: No such file or directory - path\to\file.php:12
Terminating PHP Script
I got this code from w3schools, I think you might have a need to trigger it
<?php
// A user-defined error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline) {
echo "<b>Custom error:</b> [$errno] $errstr<br>";
echo " Error on line $errline in $errfile<br>";
}
// Set user-defined error handler function
set_error_handler("myErrorHandler");
$test=2;
// Trigger error
if ($test>1) {
trigger_error("A custom error has been triggered");
}
?>
Related
I defined handler function in functions.php:
function errorHandler($errno, $errstr, $errfile, $errline) {
echo "An error: $errstr";
return true; // Preventing standard handler
}
Then I used it in set_error_handler:
require_once('functions.php');
set_error_handler("errorHandler");
But when I trigger notice, I get standard PHP message: Notice: Undefined variable: thisVariableDoesntExist in..., set_error_handler doesn't handle E_ERROR, but here I have notice, so I can't get it why this doesn't work.
I've got a PHP form with several lines of code.
Now I want to log errors in the database.
I'm trying to do this by the following code:
<?php
set_error_handler("errorHandler");
//The following line produces an error for testing
echo $notexist;
function errorHandler($errno, $errstr, $errfile, $errline) {
echo "error detected";
}
?>
Unfortunately PHP throws an error and I can't find out how to fix it:
"set_error_handler() expects the argument (errorHandler) to be a valid callback"
I think I defined the callback, didn't I?
If the function is in a namespace you need to put the fully qualified name of the function (including the namespace).
For example:
<?php
namespace MyNamespace;
set_error_handler("MyNamespace\t_errorHandler");
echo $notexist;
function t_errorHandler($errno, $errstr, $errfile, $errline) {
echo "error detected";
}
?>
Normally php script continues to run after E_NOTICE, is there a way to elevate this to fatal error in context of a function, that is I need only to exit on notice only in my functions but not on core php functions, that is globally.
You could create a custom error handler to catch E_NOTICEs.
This is untested but should go into the right direction:
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if ($errno == E_USER_NOTICE)
die ("Fatal notice");
else
return false; // Leave everything else to PHP's error handling
}
then, set it as the new custom error handler using set_error_handler() when entering your function, and restore PHP's error handler when leaving it:
function some_function()
{
// Set your error handler
$old_error_handler = set_error_handler("myErrorHandler");
... do your stuff ....
// Restore old error handler
set_error_handler($old_error_handler);
}
You use a custom error handler using set_error_handler()
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ($errno == E_USER_NOTICE) {
die("Died on user notice!! Error: {$errstr} on {$errfile}:{$errline}");
}
return false; //Will trigger PHP's default handler if reaches this point.
}
set_error_handler('myErrorHandler');
trigger_error('This is a E_USER_NOTICE level error.');
echo "This will never be executed.";
?>
Working Example
I have the following code snippet:
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL | E_NOTICE);
print $bla;
print 7;
?>
which prints out a warning that bla is undefined, but continues afterwards. I want php to throw an error and stop code execution when an undefined variable is encountered. How to do that?
The above is just an example. I want to handle EACH undefined variable within a multi thousand block clode piece.
You could write your own error handler. And make it halt execution when you encounter this type of notice. Take a look at
http://php.net/manual/en/function.set-error-handler.php
A small and simple example:
function new_error_handler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_NOTICE:
if (strstr($errstr, 'Undefined variable')) {
die('Undefined variable found');
}
break;
}
}
set_error_handler('new_error_handler');
echo $foo;
Implement an error handler with set_error_handler and put a die inside.
if(!isset($bla)){
throw new Exception('bla variable is not defined');
}
Normally php script continues to run after E_NOTICE, is there a way to elevate this to fatal error in context of a function, that is I need only to exit on notice only in my functions but not on core php functions, that is globally.
You could create a custom error handler to catch E_NOTICEs.
This is untested but should go into the right direction:
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if ($errno == E_USER_NOTICE)
die ("Fatal notice");
else
return false; // Leave everything else to PHP's error handling
}
then, set it as the new custom error handler using set_error_handler() when entering your function, and restore PHP's error handler when leaving it:
function some_function()
{
// Set your error handler
$old_error_handler = set_error_handler("myErrorHandler");
... do your stuff ....
// Restore old error handler
set_error_handler($old_error_handler);
}
You use a custom error handler using set_error_handler()
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ($errno == E_USER_NOTICE) {
die("Died on user notice!! Error: {$errstr} on {$errfile}:{$errline}");
}
return false; //Will trigger PHP's default handler if reaches this point.
}
set_error_handler('myErrorHandler');
trigger_error('This is a E_USER_NOTICE level error.');
echo "This will never be executed.";
?>
Working Example