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