当前位置:首页 > 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.     } 
  14. } 

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

错误码文件

存放目录:app/common/err

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

复用模块

针对API接口返回格式

存放目录:app/common/traits

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

页面调用:

  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自定义异常处理类

分享到: