ThinkPHP5 异常接管

  • A+
所属分类:PHP ThinkPHP5

在实际应用中,对于异常错误处理,我们往往需要进行错误的友好处理,并且需要记录错误(越为详细越好),一方面提高了用户的体验,另一方面也可以帮助开发人员更好的调试。

ThinkPHP5 异常处理说明: https://www.kancloud.cn/manual/thinkphp5/126075

首先我们在项目中自定义异常处理类:

ThinkPHP5 异常接管

在配置文件 config.php中修改配置信息如下图:

ThinkPHP5 异常接管

在 GylException.php 中进行异常接管和处理:

class GylException extends Handle
{

    public function render(Exception $e)
    {
        /*         * *******保存异常日志:::********* */
        //路由错误不记录异常
        if ($e instanceof \think\exception\RouteNotFoundException)
        {
            return parent::render($e);
        }
        //将异常错误记录在数据库表中
        $error = new ErrorLog();
        $error->editData(['error_content'=> $this->getMessage($e),'error_place'=>$e->getFile()."第".$e->getLine()."行"]);
        return parent::render($e);
    }

}

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: