• TP5接口开发之异常处理接管


    前几天在开发的时候用到了第三方的扩展包,使用过程中第三方扩展包抛出了异常

    因为这边是接口开发,需要返回错误代码以及提示信息等,所以就需要接管异常处理。

    此文章只做笔记,有不对或不详细的地方欢迎大家留言交流。

    首先先在application下创建目录exception

    然后创建BaseException类,此类为自定义异常类基类,继承框架异常类或php异常类都可。

    <?php
    /**
     * BaseException.php
     * 文件描述:自定义异常类基类,继承框架异常类
     */
    
    namespace appcommonexception;
    
    
    use thinkException;
    use Throwable;
    
    /**
     * Class BaseException
     * 自定义异常类的基类
     * @package appcommonexception
     */
    class BaseException extends Exception
    {
       public function __construct($message = "", $code = 0, Throwable $previous = null)
       {
           parent::__construct($message, $code, $previous);
       }
    }

    这里重写父类的构造方法,然后调用父类的构造方法,这块我看的有点迷,欢迎大家留言分享。

    手册上写了,框架支持异常页面由开发者自定义类进行处理,需要配置参数exception_handle

    // 异常处理handle类 留空使用 	hinkexceptionHandle
    'exception_handle'       => '\app\common\exception\Http',

    自定义类需要继承Handle并且实现render方法,手册参考:https://www.kancloud.cn/manual/thinkphp5/126075

    <?php
    /**
     * ExceptionHandler.php
     * 文件描述:重写后的异常处理类
     */
    
    namespace appcommonexception;
    
    use DawnApiexceptionUnauthorizedException;
    use Exception;
    use thinkexceptionHandle;
    use thinkLog;
    
    class ExceptionHandler extends Handle
    {
        private $code;//http状态码
        private $msg;//异常消息
        private $errorCode;//异常状态码
    
        public function render(Exception $e)
        {
            if ($e instanceof BaseException)
            {
                //使用instanceof判断异常是否为自定义异常
               $this->code = 200;
               $this->msg = $e->getMessage();
               $this->errorCode = $e->getCode();
            } elseif ($e instanceof UnauthorizedException) {
                //使用instanceof判断异常是否为第三方扩展包异常
                $this->code =200;
                $this->errorCode = 401;
                $this->msg = '授权失败';
            } else {
                //其他异常
                $this->errorCode = 10003;
                $this->msg = '操作失败';
            }
            $this->recordErrorLog($e);//将异常写入日志,方便查看
            return json(['code'=>$this->errorCode,'msg'=>$this->msg]);
        }
    
        /**
         * Notes:将异常写入日志
         * @param Exception $e
         */
        private function recordErrorLog(Exception $e) {
            Log::init([
                'type' => 'File',
                'path' => LOG_PATH,
                'level' => ['error'],
            ]);
            Log::record([
                '错误文件' => $e->getFile(),
                '错误行数' => $e->getLine(),
                '错误代码' => empty($this->code) ? $e->getCode() : $this->code,
                '错误消息' => empty($this->message) ? $e->getMessage() : $this->message,
            ], 'error');
        }
    }
  • 相关阅读:
    Airflow使用笔记
    公共关系学(第二版)笔记
    公众关系秘籍
    SQL SERVER XML 学习总结
    hadoop 1.2.1 配置
    ssh
    centos&windows的共享文件(通过挂载)
    代理设置(wget/yum)
    环境变量设置
    centos7 自定义服务
  • 原文地址:https://www.cnblogs.com/blibli/p/11168916.html
Copyright © 2020-2023  润新知