zoukankan      html  css  js  c++  java
  • ThinkPHP 全局异常处理

     wqy的笔记:http://www.upwqy.com/details/273.html

    在thinkphp6 和 thinkphp5 全局异常处理 稍有不同

    ThinkPHP6

    在 tp6 中 框架已经给出了 应用异常处理类 ExceptionHandle

    但是默认的异常处理 抛出的不是json格式的结构,不是我们想要的,所以要处理一下

    看以下代码 在 render 函数中 异常实例 $e 有两种类型 一种是BaseException  一种是 框架默认抛出的异常 

    这里主要是说 BaseException  这是自定义的异常 ,用于处理返回结构,状态码,返回信息等数据,可以按照自己的需要处理。

    具体的 返回结果返回处理 可以去 http://www.upwqy.com/details/216.html  查看

    namespace app;
    
    use appcommonApiErrCode;
    use appcommonexceptionBaseException;
    use appcommon
    esponseJsonResponse;
    use thinkdbexceptionDataNotFoundException;
    use thinkdbexceptionModelNotFoundException;
    use thinkexceptionHandle;
    use thinkexceptionHttpException;
    use thinkexceptionHttpResponseException;
    use thinkexceptionValidateException;
    use thinkResponse;
    use Throwable;
    
    /**
     * 应用异常处理类
     */
    class ExceptionHandle extends Handle
    {
        use JsonResponse;
        /**
         * 不需要记录信息(日志)的异常类列表
         * @var array
         */
        protected $ignoreReport = [
            HttpException::class,
            HttpResponseException::class,
            ModelNotFoundException::class,
            DataNotFoundException::class,
            ValidateException::class,
        ];
    
        /**
         * 记录异常信息(包括日志或者其它方式记录)
         *
         * @access public
         * @param  Throwable $exception
         * @return void
         *
         */
        public function report(Throwable $exception): void
        {
            // 使用内置的方式记录异常日志
            parent::report($exception);
        }
    
        /**
         * Render an exception into an HTTP response.
         * @access public
         * @param 	hinkRequest   $request
         * @param Throwable $e
         * @return Response
         */
        public function render($request, Throwable $e): Response
        {
            // 其他错误交给系统处理
    //        return parent::render($request, $e);
            // 添加自定义异常处理机制
    
           if($e instanceof BaseException){
               $code = $e->getCode();
               $message = $e->getMessage();
           }else{
    
               $code = $e->getCode();
               if(!$code || $code < 0){
                   $code = ApiErrCode::unknown_err['code'];
               }
               $message = $e->getMessage() ? : ApiErrCode::unknown_err['msg'];
           }
    
            return $this->jsonData($code,$message);
        }
    }
    

    下面来看 BaseException ,这里表示基础异常类 

    其中 ApiErrCode 是定义的 错误码类 可以去 http://www.upwqy.com/details/216.html 查看

    namespace appcommonexception;
    use appcommonApiErrCode;
    use thinkException;
    
    /**
     * 基础异常
     * @user yiqiu
     * @email 529857614@qq.com
     * @date 2021/2/19 20:45
     * @blog http://www.upwqy.com
     */
    class BaseException extends Exception
    {
        protected $code = ApiErrCode::unknown_err['code'];
        protected $message = ApiErrCode::unknown_err['msg'];
    
        public function __construct($params = [])
        {
            if(is_array($params) ){
    
                if(isset($params['code']) && $params['code']){
                    $this->code = $params['code'];
                }
                if(isset($params['msg']) && $params['msg']){
                    $this->message = $params['msg'];
                }
    
            }else if(is_string($params)){
                $this->message = $params;
            }
    
            parent::__construct($this->message, $this->code);
        }
    }
    

    然后我们可以自定义一些异常类 ,比如下面的 ParameterException.php 表示参数异常时的处理

    namespace appcommonexception;
    
    use appcommonApiErrCode;
    
    class ParameterException extends BaseException
    {
        protected $code = ApiErrCode::invalid_params['code'];
        protected $message = ApiErrCode::invalid_params['msg'];
    
    }
    

    实例:

     $user = User::where('id',1)->find();
            if(!$user){
                throw new ParameterException('用户不存在');
            }

    当需要指定的异常,直接 使用  throw new ParameterException('用户不存在'); 即可,返回结果如下,并且可以在任何地方使用

    {
        "code": 204,
        "msg": "用户不存在",
        "data": "",
        "timestamp": 1622604524
    }

    THinkPHP5

    在tp5框架中,我们需要手动创建应用异常处理类。ExceptionHandler.php

    并且在配置中 修改配置

    'exception_handle'      => 'apilibexceptionExceptionHandler',
  • 相关阅读:
    C语言I—2019秋作业02
    C语言I—2019秋作业01
    C语言I博客作业01
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业03
  • 原文地址:https://www.cnblogs.com/wqy415/p/14840732.html
Copyright © 2011-2022 走看看