【问题标题】:Yii2: use error handler only for fatal errors or specify handled error typesYii2:仅对致命错误使用错误处理程序或指定处理的错误类型
【发布时间】:2016-02-20 11:37:04
【问题描述】:

Yii2 有它自己的error handler,它将所有非致命的 php 错误转换为可捕获的异常。

是否可以仅将其用于处理致命错误或(更好地)明确指定哪些错误应由 yii 错误处理程序处理,哪些应由内部 php 处理程序处理?

即在开发环境中,我希望所有错误都抛出异常并提供带有跟踪的错误页面。

但在 prod 环境中,我只希望使用 yii 呈现错误页面,但通知和警告应该只进入标准 php 日志而不抛出异常。

目前,如果我将 YII_ENABLE_ERROR_HANDLER 设置为 true,我会收到通知异常(我不希望它出现在产品上);如果我将它设置为false,我会丢失 yii 致命错误页面;我将它设置为true 并将error_reporting 设置为0,我丢失了错误日志记录。

【问题讨论】:

    标签: php error-handling yii2


    【解决方案1】:

    编辑:我创建了一个package,它实现了下面描述的行为。

    Yii2 的错误处理程序不能这样配置。但是可以创建自己的错误处理程序,扩展yii\web\ErrorHandler(或yii\console\ErrorHandler,如果需要)。

    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']);
            }
        }
    
    }
    

    那么就可以配置错误处理器了:

    '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.ini.htaccess 继承服务器php 配置。

    【讨论】:

      猜你喜欢
      • 2010-12-26
      • 2016-08-18
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多