当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp5.1 中使用自定义异常处理类进行接管

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-06 10:26:39 浏览: 评论:0 

这篇文章主要介绍了thinkphp5.1 中使用自定义异常处理类进行接管,本文通过配置文件的修改和具体代码实现详细展开的讲解了如何使用,以下就是详细内容,需要的朋友可以参考下。

配置文件修改

config/app.php

自定义异常接管类出处

'exception_handle' => ‘\app\common\exception\ExceptionHandle',

自定义处理异常方法

寡人的存放目录为 app/common/exception

ApiException.php

  1. namespace app\common\exception; 
  2. use Exception; 
  3. class ApiException extends Exception 
  4. {    
  5.     /** 
  6.      * 构造函数 
  7.      */ 
  8.     public function __construct(array $ApiErrConst, Throwable $previous = null) 
  9.     { 
  10.         $code = $ApiErrConst[0]; 
  11.         $message = $ApiErrConst[1];  
  12.         parent::__construct($message$code$previous); 
  13.     } 

ExceptionHandle.php

  1. namespace app\common\exception; 
  2. use Exception; 
  3. use think\exception\Handle; 
  4. use app\common\exception\ApiException; 
  5. use app\common\err\ApiErrCode; 
  6. class ExceptionHandle extends Handle 
  7.     // 引入复用模块:JSON返回格式 
  8.     use \app\common\traits\ResponseJson;  
  9.     public function render(Exception $e
  10.     {        
  11.         if($e instanceof ApiException) { 
  12.             $code = $e->getCode(); 
  13.             $message = $e->getMessage();  
  14.         }else
  15.             $code = $e->getCode(); 
  16.             if(!$code || $code < 0) { 
  17.                 $code = ApiErrCode::ERROR_UNKNOW[0]; 
  18.             } 
  19.             $message = $e->getMessage() ? $e->getMessage() : ApiErrCode::ERROR_UNKNOW[1]; 
  20.         }        
  21.         echo $this->jsonErrorData($code,$message); //该方法在下方 
  22.         // 其他错误交给系统处理 
  23.        // return parent::render($e);  
  24.     } 

错误码文件

存放目录:app/common/err

  1. namespace app\common\err; 
  2. class ApiErrCode  
  3.     /**  
  4.      * API通用错误码 const 定义常量 
  5.      * error_code < 1000 
  6.      */ 
  7.     const ERROR_UNKNOW = [0, "未知错误"]; 
  8.     const ERROR_URL = [1, "接口不存在"]; 
  9.     ....... 

复用模块

针对API接口返回格式

存放目录:app/common/traits

  1. namespace app\common\traits; 
  2. trait ResponseJson 
  3.     /** 
  4.      * API接口出现业务异常时时返回 
  5.      * @author Leo  
  6.      */ 
  7.     public function jsonErrorData($code,$message,$data = []) 
  8.     { 
  9.         return $this->jsonResponse($code$message$data); 
  10.     } 
  11.  
  12.     /** 
  13.      * API接口请求成功时返回 
  14.      * @author Leo  
  15.      */ 
  16.     public function jsonSuccessData($data = []) 
  17.     { 
  18.         return $this->jsonResponse(200, "Sucess"$data); 
  19.     } 
  20.  
  21.     /** 
  22.      * 返回一个JSON 
  23.      * @author Leo  
  24.      */ 
  25.     private function jsonResponse($code,$message,$data)  
  26.     { 
  27.         $content = [ 
  28.             'code' => $code
  29.             'msg' => $message
  30.             'data' => $data 
  31.         ]; 
  32.         return json_encode($content); 
  33.     } 

页面调用:

  1. // 文件头部引入 
  2. use app\common\exception\ApiException; 
  3. use app\common\err\ApiErrCode; 
  4. // 引入复用模块:JSON返回格式 
  5. use \app\common\traits\ResponseJson; 
  6.  
  7. // 示例 
  8. public function index() { 
  9.     throw new ApiException(ApiErrCode::ERROR_URL);      // 自定义异常抛出 
  10. }

Tags: thinkphp5.1自定义异常处理类

分享到: