<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                在處理邏輯代碼中我們經常會碰到邏輯判斷提示 或者token無效,操作失敗,服務內部錯誤,這個時候我們需要停止程序并且返回提示和狀態碼 1.新建異常基類 extend/exception/BaseException.php ``` <?php /** * Created by PhpStorm. * User: Pasa吳 * Date: 2022/6/5 * Time: 12:46 */ namespace exception; use think\Exception; class BaseException extends Exception { public $code = 200; public $msg = 'invalid parameters'; public $errorCode = 0; public $data = ''; /** * 構造函數,接收一個關聯數組 * @param array $params 關聯數組只應包含code、msg和errorCode,且不應該是空值 */ public function __construct($params = []) { if (!is_array($params)) { return; } if (array_key_exists('code', $params)) { $this->code = $params['code']; } if (array_key_exists('msg', $params)) { $this->msg = $params['msg']; $this->message = $params['msg']; } if (array_key_exists('message', $params)) { $this->message = $params['message']; } if (array_key_exists('errorCode', $params)) { $this->errorCode = $params['errorCode']; } if (array_key_exists('data', $params)) { $this->data = $params['data']; } } } ``` 2.新建API異常類 extend/exception/ApiException.php ``` <?php /** * Created by PhpStorm. * User: Pasa吳 * Date: 2022/6/5 * Time: 12:46 */ namespace exception; use think\Exception; class BaseException extends Exception { public $code = 200; public $msg = 'invalid parameters'; public $errorCode = 0; public $data = ''; /** * 構造函數,接收一個關聯數組 * @param array $params 關聯數組只應包含code、msg和errorCode,且不應該是空值 */ public function __construct($params = []) { if (!is_array($params)) { return; } if (array_key_exists('code', $params)) { $this->code = $params['code']; } if (array_key_exists('msg', $params)) { $this->msg = $params['msg']; $this->message = $params['msg']; } if (array_key_exists('message', $params)) { $this->message = $params['message']; } if (array_key_exists('errorCode', $params)) { $this->errorCode = $params['errorCode']; } if (array_key_exists('data', $params)) { $this->data = $params['data']; } } } ``` 3.異常處理類 extend/exception/Http.php ``` <?php /** * Created by PhpStorm. * User: Pasa吳 * Date: 2022/6/5 * Time: 13:18 */ namespace exception; use think\exception\Handle; use think\exception\HttpException; use think\exception\ValidateException; use think\facade\Request; use think\Response; use Throwable; class Http extends Handle { public function render($request, Throwable $e): Response { // 添加自定義異常處理機制 if ($e instanceof BaseException) { //如果是自定義異常,則控制http狀態碼,不需要記錄日志 //因為這些通常是因為客戶端傳遞參數錯誤或者是用戶請求造成的異常 //不應當記錄日志 $this->code = $e->code; $this->msg = $e->msg; $this->errorCode = $e->errorCode; $result = ['code' => $this->errorCode, 'msg' => $this->msg, 'data' => $e->data]; return json($result, $this->code); } // 參數驗證錯誤 if ($e instanceof ValidateException) { $result = ['code' => $e->getCode(), 'msg' => $e->getError(), 'data' => []]; return json($result, 200); } // 請求異常 if ($e instanceof HttpException) { $this->msg = $e->getMessage(); $this->code = 200; $this->errorCode = 0; $result = ['code' => $this->errorCode, 'msg' => $this->msg, 'data' => Request::instance()->url()]; return json($result, $this->code); } // 其他錯誤交給系統處理 return parent::render($request, $e); } } ``` 4.異常處理接管 TP6框架支持異常處理由開發者自定義類進行接管,需要在app目錄下面的provider.php文件中綁定異常處理類 ``` <?php use app\ExceptionHandle; use app\Request; // 容器Provider定義文件 return [ 'think\Request' => Request::class, 'think\exception\Handle' => \exception\Http::class, ]; ``` 5.拋異常 ``` <?php /** * Created by PhpStorm. * User: Pasa吳 * Date: 2022/6/4 * Time: 23:19 */ namespace app\api\controller; use curd\BuildCurd; use exception\ServiceException; class Test { public function test() { throw new ServiceException(['異常提示']); } } ``` ———————————————— 版權聲明:本文為CSDN博主「Pasa吳」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/qqq13500169/article/details/125131202
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看