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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                在base.php文件中,用一句代碼\\think\\Error::register();實現錯誤和異常處理機制的注冊。 // 注冊錯誤和異常處理機制 ~~~ \think\Error::register(); ~~~ thinkphp\library\think\Error.php ``` namespace think; use think\console\Output as ConsoleOutput; use think\exception\ErrorException; use think\exception\Handle; use think\exception\ThrowableError; class Error { /** * 注冊異常處理 * @access public * @return void */ public static function register() { error_reporting(E_ALL); /* 指定appError來處理系統錯誤,捕獲錯誤后把錯誤以異常的形式拋出。 當程序出現錯誤的時候自動調用appError函數, 該函數實例化了一個PHP自帶的錯誤異常類ErrorException, 如果符合異常處理的,就將錯誤信息以異常的形式拋出來,否則將錯誤信息寫入日志中 */ set_error_handler([__CLASS__, 'appError']); /* 指定appException來處理用戶拋出的異常, 如果不是異常,就實例化ThrowableError類,將異常包裝起來并拋出 */ set_exception_handler([__CLASS__, 'appException']); /* 指定appShutdown處理超時異常。 注冊一個會在php中止時執行的函數 注冊一個callback,它會在腳本執行完成或者exit()后被調用。 */ register_shutdown_function([__CLASS__, 'appShutdown']); } /** * 異常處理 * @access public * @param \Exception|\Throwable $e 異常 * @return void */ public static function appException($e) { if (!$e instanceof \Exception) { $e = new ThrowableError($e); } //獲取handle類默認handle類,你也可以在配置件文件的exception_handle項設置自己的handle類 $handler = self::getExceptionHandler(); $handler->report($e); //將錯誤記錄到日志 if (IS_CLI) { $handler->renderForConsole(new ConsoleOutput, $e);//輸出錯誤日志到cli窗口 } else { $handler->render($e)->send();//輸出錯誤日志到頁面 } } /** * 錯誤處理 * @access public * @param integer $errno 錯誤編號 * @param integer $errstr 詳細錯誤信息 * @param string $errfile 出錯的文件 * @param integer $errline 出錯行號 * @return void * @throws ErrorException */ public static function appError($errno, $errstr, $errfile = '', $errline = 0) { $exception = new ErrorException($errno, $errstr, $errfile, $errline); // 符合異常處理的則將錯誤信息托管至 think\exception\ErrorException if (error_reporting() & $errno) { throw $exception; } self::getExceptionHandler()->report($exception); } /** * 異常中止處理 * @access public * @return void */ public static function appShutdown() { // 將錯誤信息托管至 think\ErrorException if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) { self::appException(new ErrorException( $error['type'], $error['message'], $error['file'], $error['line'] )); } // 寫入日志 Log::save(); } /** * 確定錯誤類型是否致命 * @access protected * @param int $type 錯誤類型 * @return bool */ protected static function isFatal($type) { return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]); } /** * 獲取異常處理的實例 * @access public * @return Handle */ public static function getExceptionHandler() { static $handle; if (!$handle) { // 異常處理 handle $class = Config::get('exception_handle'); if ($class && is_string($class) && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle") ) { $handle = new $class; } else { $handle = new Handle; if ($class instanceof \Closure) { $handle->setRender($class); } } } return $handle; } } ``` thinkphp\library\think\exception\Handle.php ``` namespace think\exception; use Exception; use think\App; use think\Config; use think\console\Output; use think\Lang; use think\Log; use think\Response; class Handle { protected $render; protected $ignoreReport = [ '\\think\\exception\\HttpException', ]; public function setRender($render) { $this->render = $render; } /** * Report or log an exception. * * @param \Exception $exception * @return void */ public function report(Exception $exception) { if (!$this->isIgnoreReport($exception)) { // 收集異常數據 if (App::$debug) { $data = [ 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $this->getMessage($exception), 'code' => $this->getCode($exception), ]; $log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]"; } else { $data = [ 'code' => $this->getCode($exception), 'message' => $this->getMessage($exception), ]; $log = "[{$data['code']}]{$data['message']}"; } if (Config::get('record_trace')) { $log .= "\r\n" . $exception->getTraceAsString(); } Log::record($log, 'error'); } } protected function isIgnoreReport(Exception $exception) { foreach ($this->ignoreReport as $class) { if ($exception instanceof $class) { return true; } } return false; } /** * Render an exception into an HTTP response. * * @param \Exception $e * @return Response */ public function render(Exception $e) { if ($this->render && $this->render instanceof \Closure) { $result = call_user_func_array($this->render, [$e]); if ($result) { return $result; } } if ($e instanceof HttpException) { return $this->renderHttpException($e); } else { return $this->convertExceptionToResponse($e); } } /** * @param Output $output * @param Exception $e */ public function renderForConsole(Output $output, Exception $e) { if (App::$debug) { $output->setVerbosity(Output::VERBOSITY_DEBUG); } $output->renderException($e); } /** * @param HttpException $e * @return Response */ protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); $template = Config::get('http_exception_template'); if (!App::$debug && !empty($template[$status])) { return Response::create($template[$status], 'view', $status)->assign(['e' => $e]); } else { return $this->convertExceptionToResponse($e); } } /** * @param Exception $exception * @return Response */ protected function convertExceptionToResponse(Exception $exception) { // 收集異常數據 if (App::$debug) { // 調試模式,獲取詳細的錯誤信息 $data = [ 'name' => get_class($exception), 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $this->getMessage($exception), 'trace' => $exception->getTrace(), 'code' => $this->getCode($exception), 'source' => $this->getSourceCode($exception), 'datas' => $this->getExtendData($exception), 'tables' => [ 'GET Data' => $_GET, 'POST Data' => $_POST, 'Files' => $_FILES, 'Cookies' => $_COOKIE, 'Session' => isset($_SESSION) ? $_SESSION : [], 'Server/Request Data' => $_SERVER, 'Environment Variables' => $_ENV, 'ThinkPHP Constants' => $this->getConst(), ], ]; } else { // 部署模式僅顯示 Code 和 Message $data = [ 'code' => $this->getCode($exception), 'message' => $this->getMessage($exception), ]; if (!Config::get('show_error_msg')) { // 不顯示詳細錯誤信息 $data['message'] = Config::get('error_message'); } } //保留一層 while (ob_get_level() > 1) { ob_end_clean(); } $data['echo'] = ob_get_clean(); ob_start(); extract($data); include Config::get('exception_tmpl'); // 獲取并清空緩存 $content = ob_get_clean(); $response = new Response($content, 'html'); if ($exception instanceof HttpException) { $statusCode = $exception->getStatusCode(); $response->header($exception->getHeaders()); } if (!isset($statusCode)) { $statusCode = 500; } $response->code($statusCode); return $response; } /** * 獲取錯誤編碼 * ErrorException則使用錯誤級別作為錯誤編碼 * @param \Exception $exception * @return integer 錯誤編碼 */ protected function getCode(Exception $exception) { $code = $exception->getCode(); if (!$code && $exception instanceof ErrorException) { $code = $exception->getSeverity(); } return $code; } /** * 獲取錯誤信息 * ErrorException則使用錯誤級別作為錯誤編碼 * @param \Exception $exception * @return string 錯誤信息 */ protected function getMessage(Exception $exception) { $message = $exception->getMessage(); if (IS_CLI) { return $message; } if (strpos($message, ':')) { $name = strstr($message, ':', true); $message = Lang::has($name) ? Lang::get($name) . strstr($message, ':') : $message; } elseif (strpos($message, ',')) { $name = strstr($message, ',', true); $message = Lang::has($name) ? Lang::get($name) . ':' . substr(strstr($message, ','), 1) : $message; } elseif (Lang::has($message)) { $message = Lang::get($message); } return $message; } /** * 獲取出錯文件內容 * 獲取錯誤的前9行和后9行 * @param \Exception $exception * @return array 錯誤文件內容 */ protected function getSourceCode(Exception $exception) { // 讀取前9行和后9行 $line = $exception->getLine(); $first = ($line - 9 > 0) ? $line - 9 : 1; try { $contents = file($exception->getFile()); $source = [ 'first' => $first, 'source' => array_slice($contents, $first - 1, 19), ]; } catch (Exception $e) { $source = []; } return $source; } /** * 獲取異常擴展信息 * 用于非調試模式html返回類型顯示 * @param \Exception $exception * @return array 異常類定義的擴展數據 */ protected function getExtendData(Exception $exception) { $data = []; if ($exception instanceof \think\Exception) { $data = $exception->getData(); } return $data; } /** * 獲取常量列表 * @return array 常量列表 */ private static function getConst() { return get_defined_constants(true)['user']; } } ``` # **重寫Handle類** 由上可知錯誤將會由appShutdown、appError與appException接管, 并由Handle或者自定義Handle類report選擇組裝并輸出錯誤 name我們試著自定義錯誤處理類重寫render 首先修改`exception_handle` ~~~ 'exception_handle'=>'app\lib\exception\ExceptionHandle' ~~~ 然后新建application/lib/exception/ExceptionHandler.php 負責渲染錯誤信息 ~~~ namespace app\lib\exception; use think\Exception; use think\exception\Handle; use think\Request; /** * 注意: * tp默認調用的異常處理類是think\exception\Handle; * 調用異常處理類可以在config.php配置默認為空 'exception_handle'=>'',調用的是 think\exception\Handle * 默認調用application/lib/exception/下的這個類則需要修改配置為:'exception_handle'=>'app\lib\exception\ExceptionHandle' * */ class ExceptionHandler extends Handle{ private $code;//Http Code private $msg; private $errorCode; public function render(Exception $e){ if ($e instanceof BaseException) {//!!!!如果BaseException 與這個Exceptionhandler不是同一個命名空間一定要引入空間啊 $this->code=$e->code; $this->msg=$e->message; $this->errorCode=$e->errorCode; }else{ $this->code=500; $this->msg="服務器內部錯誤"; $this->errorCode=999;//自定義的哦         //這里手動記錄日志 } $request=Request::instance(); $result=array( 'msg'=>$this->msg, 'error_code'=>$this->errorCode, 'request_url'=>$request->url() ); return json($result); } } ~~~ **為應用定義一個公共異常類** 然后新建application/lib/exception/BaseException.php baseException定義code msg errorCode這三個屬性的默認值 ~~~ namespace app\lib\exception; use think\Exception; class BaseException extends Exception{ //將錯誤代碼設為http狀態碼,業務錯誤代碼設為errorCode public $code=400; //錯誤的具體信息 public $message="參數錯誤"; //自定義的業務錯誤碼 public $errorCode="10000"; } //基類屬性參考 protected string $message ;//異常消息內容 protected int $code ;//異常代碼 protected string $file ;//拋出異常的文件名 protected int $line ;//拋出異常在該文件中的行號 ~~~ **每個模塊每個錯誤類型定義一個異常類繼承BaseException** application/lib/exception/BannerMissException.php ~~~ namespace app\lib\exception; //use app\lib\exception\Handle; class BannerMissException extends BaseException{ //http狀態碼 public $code=404; //錯誤的具體信息 public $message="請求的Banner不存在"; //自定義的錯誤碼 public $errorCode=40000; } ~~~ ## **用法:** 在model中查詢數據并在banner控制器調用 當model中沒有數據時調用BannermissException 這時exceptionHandler的render將捕獲這個錯誤 **模塊** ~~~ namespace app\api\Model; use think\Model; // use think\Excption; class Banner extends Model{ public static function getBannerById($id){ //TODO:根據bannerid號獲取banner信息 return null; //return "this is banner info"; } } ~~~ **控制器** ~~~ namespace app\api\controller\v1; use think\Controller; use app\api\Model\Banner as BannerModel;//這里的Banner和Model的Banner重名 use app\lib\exception\BannerMissException; class Banner extends controller{ public function index(){ //http://localhost/thinkphp5/public/index.php/api/v1.Banner/index } public function getBanner($id){ $banner=BannerModel::getBannerById($id); if (!$banner) { throw new BannerMissException(); } } } ~~~ ![](https://img.kancloud.cn/5b/d9/5bd955fdbed305bded89d94826c65ec1_800x313.png)
                  <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>

                              哎呀哎呀视频在线观看