feat(handler): 补充 restful 响应的状态码

This commit is contained in:
李东云
2023-04-12 17:36:15 +08:00
parent 11315aa109
commit e69c1494c3

View File

@@ -31,6 +31,8 @@ use RedisException;
use Singularity\HDK\Core\Constants\CommonErrorCode;
use Singularity\HDK\Core\Exceptions\ValidateException;
use Symfony\Component\Mailer\Exception\TransportException;
use Teapot\StatusCode\All;
use Teapot\StatusCode\RFC\RFC4918;
use Teapot\StatusCode\RFC\RFC7231;
use Throwable;
@@ -93,9 +95,11 @@ class CommonHandler extends ExceptionHandler
}
// 验证失败
if ($throwable instanceof BadRequestHttpException) {
$status_code = RFC4918::UNPROCESSABLE_ENTITY;
$data = [$code_name => CommonErrorCode::REQUEST_PARAMS_ERROR, $message_name => $is_testing ? $throwable->getMessage() : CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR)];
}
if ($throwable instanceof ValidationException) {
$status_code = RFC4918::UNPROCESSABLE_ENTITY;
$data = $throwable->validator->errors()->first();
if (is_numeric($data)) {
$code = (int) $data;
@@ -104,6 +108,7 @@ class CommonHandler extends ExceptionHandler
$data = [$code_name => $code ?? CommonErrorCode::REQUEST_PARAMS_ERROR, $message_name => $data];
}
if ($throwable instanceof ValidateException) {
$status_code = RFC4918::UNPROCESSABLE_ENTITY;
$code = $throwable->getCode();
$message = $throwable->getMessage();
$data = [$code_name => $code, $message_name => empty($message) ? CommonErrorCode::getMessage($code, ['param' => $throwable->getFieldName()]) : $message];
@@ -116,14 +121,14 @@ class CommonHandler extends ExceptionHandler
if ($throwable instanceof NotFoundHttpException) {
$code = CommonErrorCode::ROUTE_NOT_FOUND;
$data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($code)];
$status_code = 404;
$status_code = RFC7231::NOT_FOUND;
}
// 模型不存在
if ($throwable instanceof ModelNotFoundException) {
$code = empty($throwable->getCode()) ? CommonErrorCode::MODEL_NOT_FOUND : $throwable->getCode();
$message = empty($throwable->getCode()) ? CommonErrorCode::getMessage($code, ['resource' => '资源']) : $throwable->getMessage();
$data = [$code_name => $code, $message_name => $message];
$status_code = 404;
$status_code = RFC7231::NOT_FOUND;
}
// 300 服务出错
// 303 缓存异常