feat(common.exception): 对验证的exception添加了 message 的处理

This commit is contained in:
李东云
2022-10-08 16:23:25 +08:00
parent 3f3db0ea68
commit f18892477c
4 changed files with 13 additions and 4 deletions

View File

@@ -77,7 +77,7 @@ class CommonErrorCode extends AbstractConstants
/**
* 参数非法.
* @Message("common_error.params.format_common_error")
* @Message("common_error.params.format_error")
*/
public const FORMATTER_ERROR = 1020001;

View File

@@ -14,6 +14,7 @@ class DbException extends HttpException
{
public function __construct(
int $code = CommonErrorCode::PROGRAM_SQL_ERROR,
?string $message = null,
public string $sql = '',
Throwable $previous = null
) {
@@ -23,7 +24,7 @@ class DbException extends HttpException
}
parent::__construct(
RFC7231::INTERNAL_SERVER_ERROR,
CommonErrorCode::getMessage($code),
$message ?? CommonErrorCode::getMessage($code),
$code,
$previous
);

View File

@@ -156,9 +156,10 @@ class CommonHandler extends ExceptionHandler
}
if ($throwable instanceof ValidateException) {
$code = $throwable->getCode();
$message = $throwable->getMessage();
$data = [
$code_name => $code,
$message_name => CommonErrorCode::getMessage($code, [
$message_name => $message ?? CommonErrorCode::getMessage($code, [
'param' => $throwable->getFieldName(),
]),
];

View File

@@ -14,6 +14,7 @@ class ValidateException extends HttpException
{
public function __construct(
int $code = CommonErrorCode::FORMATTER_ERROR,
?string $message = null,
private string $field = '',
private mixed $currentValue = null,
private array $availableValue = [],
@@ -25,7 +26,13 @@ class ValidateException extends HttpException
}
parent::__construct(
RFC4918::UNPROCESSABLE_ENTITY,
CommonErrorCode::getMessage($code),
$message ??
CommonErrorCode::getMessage(
$code,
[
'param' => $this->getFieldName(),
]
),
$code,
$previous
);