Stop Script Execution Upon Notice/Warning

Stop script execution upon notice/warning

Yes, it is possible. This question speaks to the more general issue of how to handle errors in PHP. You should define and register a custom error handler using set_error_handlerdocs to customize handling for PHP errors.

IMHO it's best to throw an exception on any PHP error and use try/catch blocks to control program flow, but opinions differ on this point.

To accomplish the OP's stated goal you might do something like:

function errHandle($errNo, $errStr, $errFile, $errLine) {
$msg = "$errStr in $errFile on line $errLine";
if ($errNo == E_NOTICE || $errNo == E_WARNING) {
throw new ErrorException($msg, $errNo);
} else {
echo $msg;
}
}

set_error_handler('errHandle');

The above code will throw an ErrorException any time an E_NOTICE or E_WARNING is raised, effectively terminating script output (if the exception isn't caught). Throwing exceptions on PHP errors is best combined with a parallel exception handling strategy (set_exception_handler) to gracefully terminate in production environments.

Note that the above example will not respect the @ error suppression operator. If this is important to you, simply add a check with the error_reporting() function as demonstrated here:

function errHandle($errNo, $errStr, $errFile, $errLine) {
if (error_reporting() == 0) {
// @ suppression used, don't worry about it
return;
}
// handle error here
}

Simply stop script execution upon notice/warning

You can create a custom error handling function, like so:

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}

switch ($errno) {
case E_USER_ERROR: exit('Im a user error.');
case E_USER_WARNING: exit('Im a user warning');
case E_USER_NOTICE:
printNotice($errno, $errstr, $errfile, $errline);
break;
default: exit('Unknown Error');
}

// don't execute PHP internal error handler
return true;
}

function printNotice($errno, $errstr, $errfile, $errline)
{
// use the following vars to output the original error
var_dump($errno, $errstr, $errfile, $errline);

exit;
}

Important are the constants: E_USER_NOTICE and E_USER_WARNING.

The function printNotice() shows how you can print the original error,
so that it appears unmodified and then stops script execution with exit.
All the original error data (message, line number, etc.) is in the variables
$errno, $errstr, $errfile, $errline. PHP makes the error data automatically available at the registered errorhandler (here myErrorHandler).
In this example i'm forwarding all the parameters a second time, to printNotice() function, which could format the error differently or do what you like upon it.

And then register it with set_error_handler(), like so:

<?php
// register your error handler
set_error_handler('myErrorHandler');

In order to test the error handling you might use the function trigger_error():

trigger_error("This is a User Error", E_USER_ERROR);
trigger_error("This is a User Warning", E_USER_WARNING);
trigger_error("This is a User Warning", E_USER_NOTICE);

A configuration to stop PHP execution an any warnings or exceptions

From what I can tell, there is no way to do this outside of creating a custom error handler that stops execution immediately. However, it would only take about four lines to do that (or use the answers/comments that SomeKittens posted).

Sorry to say but the answer to your questions is "no."

PHP Script stops processing on a warning error?

Don't know how to continue on errors, but the better thing would be error prevention in first place:

http://php.net/manual/en/function.file-exists.php

http://www.php.net/manual/en/function.is-readable.php

Stop script execution upon notice/warning

Yes, it is possible. This question speaks to the more general issue of how to handle errors in PHP. You should define and register a custom error handler using set_error_handlerdocs to customize handling for PHP errors.

IMHO it's best to throw an exception on any PHP error and use try/catch blocks to control program flow, but opinions differ on this point.

To accomplish the OP's stated goal you might do something like:

function errHandle($errNo, $errStr, $errFile, $errLine) {
$msg = "$errStr in $errFile on line $errLine";
if ($errNo == E_NOTICE || $errNo == E_WARNING) {
throw new ErrorException($msg, $errNo);
} else {
echo $msg;
}
}

set_error_handler('errHandle');

The above code will throw an ErrorException any time an E_NOTICE or E_WARNING is raised, effectively terminating script output (if the exception isn't caught). Throwing exceptions on PHP errors is best combined with a parallel exception handling strategy (set_exception_handler) to gracefully terminate in production environments.

Note that the above example will not respect the @ error suppression operator. If this is important to you, simply add a check with the error_reporting() function as demonstrated here:

function errHandle($errNo, $errStr, $errFile, $errLine) {
if (error_reporting() == 0) {
// @ suppression used, don't worry about it
return;
}
// handle error here
}

How to automatically stop any script and redirect to an error page if an error/warning/notice is uncaught

Thanks to @MladenB. and deceze, I solved my problem. This is how I coded the solution:

in a config.php file, to be included in your scripts (it's better to move the functions to a personal library file):

<?php

function my_error_handler($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

function my_exception_handler($e)
{
/**
* Exception handler that pass the error object to an error page.
* This is to avoid bad displaying or hiding of error reports.
*
* @param $e Exception The exception to manage
*/

if (session_status() !== PHP_SESSION_ACTIVE)
{
session_start();
}

session_register_shutdown();

$_SESSION['error'] = $e;

header('Location: error.php');
exit();
}

set_error_handler('my_error_handler');
set_exception_handler('my_exception_handler');

in error.php:

<?php

session_start();
session_register_shutdown();

$e = $_SESSION['error'];

echo '<h2>Stack trace</h2>';
echo var_dump($e->getTrace());

throw $e;

Php, is there a way to turn all notices/warnings into an exception?

Yes.
this is exactly why the ErrorException was created. see http://php.net/manual/en/class.errorexception.php

function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
// This error code is not included in error_reporting
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");

Remove warning messages in PHP

You really should fix whatever's causing the warning, but you can control visibility of errors with error_reporting(). To skip warning messages, you could use something like:

error_reporting(E_ERROR | E_PARSE);

How to terminate script on any error? (Like the equivalent bash `set -e` option)

This seems to do the job:

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});


Related Topics



Leave a reply



Submit