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

吾爱主题 阅读:145 2021-11-19 16:19:00 评论:0

配置文件修改

config/app.php
自定义异常接管类出处
'exception_handle' => ‘\app\common\exception\ExceptionHandle',

自定义处理异常方法
寡人的存放目录为 app/common/exception

ApiException.php

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

ExceptionHandle.php

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

错误码文件

存放目录:app/common/err

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

复用模块

针对API接口返回格式
存放目录:app/common/traits

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

页面调用

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

到此这篇关于thinkphp5.1 中使用自定义异常处理类进行接管的文章就介绍到这了,更多相关thinkphp5.1 自定义异常处理类内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/u011867622/article/details/97147228

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容