Yii2:仅对致命错误使用错误处理程序,或指定处理过的错误类型 [英] Yii2: use error handler only for fatal errors or specify handled error types

查看:1035
本文介绍了Yii2:仅对致命错误使用错误处理程序,或指定处理过的错误类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Yii2拥有自己的错误处理程序,即将所有非致命的PHP错误转换为可捕获的异常。

Yii2 has it's own error handler, that converts all non-fatal php errors into catchable exceptions.

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

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

在开发环境中,我想让所有的错误都抛出一个异常并提供错误页面跟踪。

I.e. in dev environment I want all errors to throw an exceptions and provide error page with trace.

但是在prod环境中,我只想要使用yii渲染错误页面的致命错误,但是通知和警告应该直接进入标准的php日志,而不会丢弃。

But in prod environment I want only fatal errors to render error page with yii, but notices and warnings should just go to standard php log without throwing an exeption.

目前,如果我将 YII_ENABLE_ERROR_HANDLER code> true ,我在通知中收到异常(我不想在prod上);如果我将它设置为 false ,我放弃了yii致命错误页面;并将它设置为 true 并将 error_reporting 设置为 0 ,我松动错误日志。

Currently if I set YII_ENABLE_ERROR_HANDLER to true, I get exception on notices (I do not want it on prod); if I set it to false, I loose yii fatal error pages; and it I set it to true and set error_reporting to 0, I loose error logging.

推荐答案

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

I created a package that implements behavior described below.

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

Yii2's error handler cannot be configured in this way. But it is possible to create own error handler, extending 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 it is possible to 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错误处理程序处理,如果您配置它将显示在错误日志中。

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

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

这篇关于Yii2:仅对致命错误使用错误处理程序,或指定处理过的错误类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆