为了账号安全,请及时绑定邮箱和手机立即绑定

Error.php

标签:
PHP

<?php

// +----------------------------------------------------------------------

// | ThinkPHP [ WE CAN DO IT JUST THINK ]

// +----------------------------------------------------------------------

// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>

// +----------------------------------------------------------------------

 

namespace think;

 

use think\console\Output as ConsoleOutput;// 支持 控制台 输出

use think\exception\ErrorException;// 错误 异常

use think\exception\Handle;// 手柄 处理 助手

use think\exception\ThrowableError;// 可抛出 错误

// namespace

// 使用 各种 错误处理机制

class Error

{// 公共 错误 异常

    /**

     * 注册异常处理

     * @return void

     */

    public static function register()

    {// 注册 全部错误,

        error_reporting(E_ALL);// 设置 错误 级别

        set_error_handler([__CLASS__, 'appError']);// 设置 错误处理方式

        set_exception_handler([__CLASS__, 'appException']);// 设置异常 处理

        register_shutdown_function([__CLASS__, 'appShutdown']);// 注册关闭 函数

    }

 

    /**

     * Exception Handler

     * @param  \Exception|\Throwable $e

     */

    public static function appException($e)

    {// Exception Handler 错误处理 代码

        if (!$e instanceof \Exception) {

            $e = new ThrowableError($e);// 如果 发送 过来的不是 异常,包装一下,然后 抛出

        }

 

        self::getExceptionHandler()->report($e);// 获取处理 助手,然后 进行 $e 发送

        if (IS_CLI) {// 是否 命令行 模式

            self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);// 渲染问题

        } else {

            self::getExceptionHandler()->render($e)->send();// 发送 渲染

        }

    }

 

    /**

     * Error Handler

     * @param  integer $errno   错误编号

     * @param  integer $errstr  详细错误信息

     * @param  string  $errfile 出错的文件

     * @param  integer $errline 出错行号

     * @param array    $errcontext

     * @throws ErrorException

     */

    public static function appError($errno, $errstr, $errfile = '', $errline = 0, $errcontext = [])

    {//

        $exception = new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);

        if (error_reporting() & $errno) {

            // 将错误信息托管至 think\exception\ErrorException

            throw $exception;

        } else {

            self::getExceptionHandler()->report($exception);

        }// 展示错误

    }

 

    /**

     * Shutdown Handler

     */

    public static function appShutdown()

    {

        if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {

            // 将错误信息托管至think\ErrorException

            $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);

 

            self::appException($exception);

        }// 转包 错误 日志

 

        // 写入日志

        Log::save();// 有了自动加载,简直就是上了一个新台阶啊!哈哈

    }

 

    /**

     * 确定错误类型是否致命

     *

     * @param  int $type

     * @return bool

     */

    protected static function isFatal($type)

    {

        return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);

    }// 检测是否致命错误

 

    /**

     * Get an instance of the exception handler.

     *

     * @return Handle

     */

    public static function getExceptionHandler()

    {

        static $handle;

        if (!$handle) {

            // 异常处理handle

            $class = Config::get('exception_handle');// Config::get("")

            if ($class && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) {

                $handle = new $class;// 找到对应的 类处理

            } else {

                $handle = new Handle;// 生成 对应的 Handle

            }

        }

        return $handle;

    }

}

/**

 * 总结:

 * 1 register 分别注册跟激活了 自己的 错误、异常、关闭的自动处理函数

 * 2 对应到分别为 appExeception

 * 3 对应到分别为 appError

 * 4 对应 appShutdown

 * 5 isFatal 检测 是否是致命错误,通过 错误 类型

 * 6 获取错误处理句柄

 */

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消