<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 異常處理 >本著嚴謹的原則,框架會對任何錯誤(包括警告錯誤)拋出異常。系統產生的異常和錯誤都是程序的隱患,要盡早排除和解決,而不是掩蓋。對于應用自己拋出的異常則做出相應的捕獲處理。 不同模塊處理調用相對異常處理類,詳情見【CRMEB類庫】->【Exceptions錯誤拋出】 可以整體所有應用定義統一異常處理類,`app`下或單獨應用`api`下`provider.php`注入綁定異常處理類 api應用下: ``` // 容器Provider定義文件 return [ 'think\exception\Handle' => \app\api\ApiExceptionHandle::class, ]; ``` 自定義類需要繼承`think\exception\Handle`并且實現`render`方法 如下完成類: ~~~ <?php namespace app\api; use crmeb\exceptions\ApiException; use crmeb\exceptions\AuthException; use think\exception\DbException; use think\exception\Handle; use think\facade\Env; use think\Response; use Throwable; use think\exception\ValidateException; class ApiExceptionHandle extends Handle { /** * 記錄異常信息(包括日志或者其它方式記錄) * * @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 \think\Request $request * @param Throwable $e * @return Response */ public function render($request, Throwable $e): Response { // 添加自定義異常處理機制 if ($e instanceof DbException) { return app('json')->fail('數據獲取失敗', [ 'file' => $e->getFile(), 'message' => $e->getMessage(), 'line' => $e->getLine(), ]); } else if ($e instanceof AuthException || $e instanceof ApiException || $e instanceof ValidateException) { return app('json')->fail($e->getMessage()); } else { return app('json')->fail('很抱歉!系統開小差了', Env::get('app_debug', false) ? [ 'message' => $e->getMessage(), 'file' => $e->getFile(), 'code' => $e->getCode(), 'line' => $e->getLine(), 'trace' => $e->getTrace(), 'previous' => $e->getPrevious(), ] : []); } } } ~~~ ### 手動捕獲拋和出異常 可以手動使用`throw`來拋出一個異常 ``` // 使用think自帶異常類拋出異常 throw new \\think\\Exception('異常消息', 400); ``` 手動捕獲異常方式是使用`try-catch` ``` try { // 這里是主體代碼 } catch (ValidateException $e) { // 這是進行驗證異常捕獲 return json($e->getError()); } catch (\Exception $e) { // 這是進行異常捕獲 return json($e->getMessage()); } ```
                  <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>

                              哎呀哎呀视频在线观看