Asked  7 Months ago    Answers:  5   Viewed   53 times

How can i handle parse & fatal errors using a custom error handler?

 Answers

79

Simple Answer: You can't. See the manual:

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.

For every other error, you can use set_error_handler()

EDIT:

Since it seems, that there are some discussions on this topic, with regards to using register_shutdown_function, we should take a look at the definition of handling: To me, handling an error means catching the error and reacting in a way that is "nice" for the user and the underlying data (databases, files, web services, etc.).

Using register_shutdown_function you cannot handle an error from within the code where it was called, meaning the code would still stop working at the point where the error occurs. You can, however, present the user with an error message instead of a white page, but you cannot, for example, roll back anything that your code did prior to failing.

Wednesday, March 31, 2021
 
mario
answered 7 Months ago
20

You have issue with php default and short tags.

<? ... ?>   PHP short tag
<?php ... ?> PHP default tag

See here for more details

Please replace your last few lines with following code

<?php
}
function add_bxjs() {
    /*wp_enqueue_script(
        'custom-script',
        plugins_url('/bxslider/jquery.bxslider.js', __FILE__ ) ,
        array( 'jquery' )
    );*/
    wp_enqueue_style('bxstyle', plugins_url('/bxslider/jquery.bxslider.css', __FILE__ ));
    wp_enqueue_script('jquery',plugins_url('/bxslider/jquery.min.js', __FILE__ ), array('jquery'));
    wp_enqueue_script('easing',plugins_url('/bxslider/jquery.easing.1.3.js', __FILE__ ), array('jquery'));
    wp_enqueue_script('bxscript',plugins_url('/bxslider/jquery.bxslider.js', __FILE__ ), array('jquery'));
}

add_action( 'wp_enqueue_scripts', 'add_bxjs' );
?>
Friday, May 28, 2021
 
PHLAK
answered 5 Months ago
42

EDIT: I created a package that implements behavior described below.

Yii2's error handler cannot be configured in this way. But it is possible to create own error handler, extending yiiwebErrorHandler (or yiiconsoleErrorHandler if required).

namespace appweb;

use yiiwebErrorHandler as BaseErrorHandler;

class ErrorHandler extends BaseErrorHandler {


    /**
     * @var array Used to specify which errors this handler should process.
     *
     * Default is ['fatal' => true, 'catchable' => E_ALL | E_STRICT ]
     *
     * E_ALL | E_STRICT is a default from set_error_handler() documentation.
     *
     * Set
     *     'catchable' => false
     * to disable catchable error handling with this ErrorHandler.
     *
     * You can also explicitly specify, which error types to process, i. e.:
     *     'catchable' => E_ALL & ~E_NOTICE & ~E_STRICT
     */
    public $error_types;

    /**
     * @var boolean Used to specify display_errors php ini setting
     */
    public $display_errors = false;

    /**
     * @var string Used to reserve memory for fatal error handler.
     */
    private $_memoryReserve;
    /**
     * @var Exception from HHVM error that stores backtrace
     */
    private $_hhvmException;

    /**
     * Register this error handler
     */
    public function register()
    {
        // by default process all errors
        // E_ALL | E_STRICT is a default from set_error_handler() documentation
        $default_error_types = [ 'fatal' => true, 'catchable' => E_ALL | E_STRICT ];
        // merge with application configuration
        $error_types = array_merge($default_error_types, (array) $this->error_types);

        ini_set('display_errors', $this->display_errors);
        set_exception_handler([$this, 'handleException']);
        if (defined('HHVM_VERSION')) {
            set_error_handler([$this, 'handleHhvmError'], $error_types['catchable']);
        } else {
            set_error_handler([$this, 'handleError'], $error_types['catchable']);
        }
        if ($this->memoryReserveSize > 0) {
            $this->_memoryReserve = str_repeat('x', $this->memoryReserveSize);
        }
        if ($error_types['fatal']) {
            register_shutdown_function([$this, 'handleFatalError']);
        }
    }

}

Then it is possible to configure the error handler:

'components' => [
    'errorHandler' => [
        'class' => 'appwebErrorHandler',
        'error_types' => [
            'fatal' => true,
            'catchable' => YII_DEBUG ? (E_ALL | E_STRICT) : false
        ],
        'display_errors' => ini_get('display_errors')
    ],
],

In this example configuration we say that error handler should always handle fatal errors, but handle catchable errors only if we are in debug mode. In production mode all catchable errors will be handled by internal php error handler and will appear in error log if you configure it.

display_errors is said to inherit server php configuration from php.ini or .htaccess.

Saturday, May 29, 2021
 
Fernando
answered 5 Months ago
23

Change

$data[$parts[0]] = $parts[1];

to

if ( ! isset($parts[1])) {
   $parts[1] = null;
}

$data[$parts[0]] = $parts[1];

or simply:

$data[$parts[0]] = isset($parts[1]) ? $parts[1] : null;

Not every line of your file has a colon in it and therefore explode on it returns an array of size 1.

According to php.net possible return values from explode:

Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

Wednesday, June 16, 2021
 
Webroots
answered 5 Months ago
74

Culprit has been found. It turns out that session_start() doesn't like the read method of a custom session handler returning false if there's no session in existence. if anyone else gets a similar problem, if you've no session in existence, then the read method needs to return an empty string instead of either false or null.

The solution was found at http://php.net/manual/en/function.session-start.php#120589 i was looking at the the manual page for session_set_save_handler(), not expecting the solution to actually be in the comments section of the session_start() page.

I don't know why the change was made to what the session_start() method expects to get from the read method of a custom session handler

Tuesday, October 12, 2021
 
Tomasz Mularczyk
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :