wqy的筆記:http://www.upwqy.com/details/273.html

在thinkphp6 和 thinkphp5 全域性異常處理 稍有不同

ThinkPHP6

在 tp6 中 框架已經給出了 應用異常處理類 ExceptionHandle

但是預設的異常處理 丟擲的不是json格式的結構,不是我們想要的,所以要處理一下

看以下程式碼 在 render 函式中 異常例項 $e 有兩種型別 一種是BaseException  一種是 框架預設丟擲的異常

這裡主要是說 BaseException  這是自定義的異常 ,用於處理返回結構,狀態碼,返回資訊等資料,可以按照自己的需要處理。

具體的 返回結果返回處理 可以去 http://www.upwqy.com/details/216.html  檢視

namespace app;

use app\common\ApiErrCode;
use app\common\exception\BaseException;
use app\common\response\JsonResponse;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable; /**
* 應用異常處理類
*/
class ExceptionHandle extends Handle
{
use JsonResponse;
/**
* 不需要記錄資訊(日誌)的異常類列表
* @var array
*/
protected $ignoreReport = [
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
DataNotFoundException::class,
ValidateException::class,
]; /**
* 記錄異常資訊(包括日誌或者其它方式記錄)
*
* @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
{
// 其他錯誤交給系統處理
// return parent::render($request, $e);
// 新增自定義異常處理機制 if($e instanceof BaseException){
$code = $e->getCode();
$message = $e->getMessage();
}else{ $code = $e->getCode();
if(!$code || $code < 0){
$code = ApiErrCode::unknown_err['code'];
}
$message = $e->getMessage() ? : ApiErrCode::unknown_err['msg'];
} return $this->jsonData($code,$message);
}
}

下面來看 BaseException ,這裡表示基礎異常類

其中 ApiErrCode 是定義的 錯誤碼類 可以去 http://www.upwqy.com/details/216.html 檢視

namespace app\common\exception;
use app\common\ApiErrCode;
use think\Exception; /**
* 基礎異常
* @user yiqiu
* @email [email protected]
* @date 2021/2/19 20:45
* @blog http://www.upwqy.com
*/
class BaseException extends \Exception
{
protected $code = ApiErrCode::unknown_err['code'];
protected $message = ApiErrCode::unknown_err['msg']; public function __construct($params = [])
{
if(is_array($params) ){ if(isset($params['code']) && $params['code']){
$this->code = $params['code'];
}
if(isset($params['msg']) && $params['msg']){
$this->message = $params['msg'];
} }else if(is_string($params)){
$this->message = $params;
} parent::__construct($this->message, $this->code);
}
}

然後我們可以自定義一些異常類 ,比如下面的 ParameterException.php 表示引數異常時的處理

namespace app\common\exception;

use app\common\ApiErrCode;

class ParameterException extends BaseException
{
protected $code = ApiErrCode::invalid_params['code'];
protected $message = ApiErrCode::invalid_params['msg']; }

例項:

 $user = User::where('id',1)->find();
if(!$user){
throw new ParameterException('使用者不存在');
}

當需要指定的異常,直接 使用  throw new ParameterException('使用者不存在'); 即可,返回結果如下,並且可以在任何地方使用

{
"code": 204,
"msg": "使用者不存在",
"data": "",
"timestamp": 1622604524
}

THinkPHP5

在tp5框架中,我們需要手動建立應用異常處理類。ExceptionHandler.php

並且在配置中 修改配置

'exception_handle'      => 'api\lib\exception\ExceptionHandler',