異常增強(qiáng)插件

v1.1.0
版本
2025-05-20
版本更新時(shí)間
111
安裝
1
star
簡介
該插件擴(kuò)展了webman的Handler類,webman的Handler只能對(duì)同一應(yīng)用下的異常進(jìn)行統(tǒng)一處理,而該插件可以針對(duì)同一應(yīng)用的不同異常進(jìn)行不同的處理。
安裝
composer require jnewer/exception-handler
配置
config/exception.php
return [
// 這里配置異常處理類,有單獨(dú)配置Handler的異常,會(huì)自動(dòng)分發(fā)給對(duì)應(yīng)的Handler處理
'' => \Jnewer\ExceptionHandler\Handler::class,
];
config/plugin/jnewer/exception-handler.php
use Illuminate\Validation\ValidationException;
use Jnewer\ExceptionHandler\Exception\BaseException;
use Jnewer\ExceptionHandler\BaseExceptionHandler;
use Jnewer\ExceptionHandler\ValidationExceptionHandler;
return [
'enable' => true,
'exception' => [
// 這里配置異常類和對(duì)應(yīng)的處理類
'handlers' => [
ValidationException::class => ValidationExceptionHandler::class,
BaseException::class => BaseExceptionHandler::class
],
'dont_report' => [
BusinessException::class,
BaseException::class,
ValidationException::class,
]
]
];
多應(yīng)用模式時(shí),你可以為每個(gè)應(yīng)用單獨(dú)配置異常處理類,參見多應(yīng)用
基本用法
use support\Request;
use support\Response;
use Jnewer\ExceptionHandler\Exception\NotFoundHttpException;
class UserController{
public function view($id): Response
{
$user = User::find($id);
if (is_null($user)) {
throw new NotFoundHttpException('用戶不存在');
}
}
}
以上異常拋出錯(cuò)誤信息,如下格式:
HTTP/1.1 404 Not Found
Content-Type: application/json;charset=utf-8
{
"code": 0,
"success": false,
"message": "用戶不存在",
"data": [],
}
內(nèi)置異常類
- 客戶端異常類(HTTP Status 400):NotFoundHttpException
- 身份認(rèn)證異常類(HTTP Status 401):UnauthorizedHttpException
- 資源授權(quán)異常類(HTTP Status 403):ForbiddenHttpException
- 資源未找到異常類(HTTP Status 404):NotFoundHttpException
- 請求方法不允許異常類(HTTP Status 405):MethodNotAllowedHttpException
- 請求內(nèi)容類型不支持異常類(HTTP Status 406):NotAcceptableHttpException
- 請求限流在異常類(HTTP Status 429):TooManyRequestsHttpException
- 服務(wù)器內(nèi)部錯(cuò)誤異常類(HTTP Status 500):ServerErrorHttpException
更多參考:https://datatracker.ietf.org/doc/html/rfc7231#page-47
自定義異常類
<?php
namespace support\exception;
use Jnewer\ExceptionHandler\Exception\BaseException;
class InvalidArgumentException extends BaseException
{
}
使用異常類
use support\Request;
use support\Response;
use support\exception\InvalidArgumentException;
class UserController{
public function create(Request $request): Response
{
if (!$request->post('name')) {
throw new InvalidArgumentException('參數(shù)有誤');
}
}
}
內(nèi)置異常類Handler
- 擴(kuò)展自webman的Handler:Handler,可以針對(duì)不同的異常類進(jìn)行不同的處理。
- BaseException異常處理:BaseExceptionHandler
- laravel驗(yàn)證器異常處理:ValidationExceptionHandler
自定義異常類Handler
<?php
namespace support\exception;
use support\exception\Handler;
use Illuminate\Validation\ValidationException;
use Throwable;
use function json_encode;
class InvalidArgumentExceptionHandler extends Handler
{
public function render(Request $request, Throwable $exception): Response
{
$message = $exception->getMessage();
$statusCode = $exception->statusCode ?? 500;
if (!$request->expectsJson()) {
return new Response($statusCode, [], $message);
}
$jsonMessage = ['code' => $exception->code ?: $exception->statusCode, 'message' => $message, 'success' => false, 'data' => []];
return new Response(
$statusCode,
['Content-Type' => 'application/json'],
json_encode($jsonMessage, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);
}
}
配置Handler
config/plugin/jnewer/exception-handler.php
use app\exception\InvalidArgumentException;
use app\exception\InvalidArgumentExceptionHandler;
return [
'exception' => [
// 這里配置異常類和對(duì)應(yīng)的處理類
'handlers' => [
...// 其他異常和處理類
InvalidArgumentException::class => InvalidArgumentExceptionHandler::class
]
]
];