baagee/wtf-error

There is no license information available for the latest version (v0.0.8) of this package.

What the fuck! PHP error handler

v0.0.8 2020-07-18 05:02 UTC

This package is auto-updated.

Last update: 2024-05-18 13:33:38 UTC


README

默认的php异常/错误在页面上展示的很难看,通过set_error_handler和set_exception_handler可以捕获到错误和异常,按照自己喜欢的格式去输出或者隐藏

此类稍微封装了一下两种模式下的错误输出

具体使用:

composer require baagee/wtf-error

只需要在项目开头注册一下就行了

// 使用内置的错误处理展示
\BaAGee\Wtf\WtfError::register(new \BaAGee\Wtf\Handler\WtfHandler([
    'php_error_log_dir' => __DIR__ . '/log',//指定PHP错误log目录,为空 不记录
    // 是否是开发调试模式,会显示详细错误。生产环境下很不安全,建议为false
    'is_debug'          => true
]));

截图示例:

访问页面出错时:

示例

执行脚本出错时:

示例

使用自定义的错误处理

/**
 * Class MyHandler
 */
class MyHandler extends \BaAGee\Wtf\Base\WtfHandlerAbstract
{
    /**
     * 自定义错误异常显示 必须实现
     * @param Throwable $t
     */
    public function throwableHandler(\Throwable $t)
    {
        header('content-type: application/json; charset=utf-8');
        // 输出json
        echo json_encode([
            'code'    => $t->getCode(),
            'message' => $t->getMessage(),
            'file'    => $t->getFile(),
            'line'    => $t->getLine(),
        ], JSON_UNESCAPED_UNICODE);
    }

    // 自定义写入Log 不是必须的,不用自己调用,定义好方法就会自动调用
    public function writePhpErrorLog(\Throwable $t)
    {
        file_put_contents('./log.log', $t->getMessage() . PHP_EOL, FILE_APPEND);
    }
}

\BaAGee\Wtf\WtfError::register(new MyHandler([
    'php_error_log_dir'    => __DIR__ . '/log',
    'is_debug'             => true,
    'product_error_hidden' => [E_WARNING, E_NOTICE, E_STRICT, E_DEPRECATED]
]));

输出结果:

{"code":0,"message":"Call to undefined function abc()","file":"\/Users\/baagee\/PhpstormProjects\/github\/wtf-error\/tests\/error.php","line":26}

详细见tests目录