Yii2: use error handler only for fatal errors or specify error types handled

Yii2 has its own, which converts all non-fatal php errors to catching exceptions.

Is it possible to use it only to handle fatal errors, or (better) explicitly specify which errors should be handled by the yii error handler and which should be handled by the internal php handler?

those. in dev environment I want all errors to throw exceptions and display the error page.

But in the prod environment, I want only fatal errors to display the error page with yii, but notifications and warnings should just go to the standard php logs without throwing exeption.

Currently, if I install YII_ENABLE_ERROR_HANDLERin true, I get an exception from notifications (I don't want it on prod); if I installed it on false, I lost the pages of the deadly error yii; and I installed it in trueand installed error_reportingin 0, I lost registration errors.

+4
source share
1 answer

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

The Yii2 error handler cannot be configured this way. But you can create your own error handler by expanding yii\web\ErrorHandler(or yii\console\ErrorHandler, if required).

namespace app\web;

use yii\web\ErrorHandler 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 you can configure the error handler:

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

, , , . php , .

,

display_errors php php.ini .htaccess.

0

Source: https://habr.com/ru/post/1629689/


All Articles