diff --git a/src/Utils/Constants/CommonErrorCode.php b/src/Utils/Constants/CommonErrorCode.php index 8c6d735..590b9b0 100644 --- a/src/Utils/Constants/CommonErrorCode.php +++ b/src/Utils/Constants/CommonErrorCode.php @@ -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; diff --git a/src/Utils/Exceptions/DbException.php b/src/Utils/Exceptions/DbException.php index 964098d..1843ec1 100644 --- a/src/Utils/Exceptions/DbException.php +++ b/src/Utils/Exceptions/DbException.php @@ -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 ); diff --git a/src/Utils/Exceptions/Handler/CommonHandler.php b/src/Utils/Exceptions/Handler/CommonHandler.php index 54b9084..70d666a 100644 --- a/src/Utils/Exceptions/Handler/CommonHandler.php +++ b/src/Utils/Exceptions/Handler/CommonHandler.php @@ -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(), ]), ]; diff --git a/src/Utils/Exceptions/ValidateException.php b/src/Utils/Exceptions/ValidateException.php index 7b542e9..b1dbba3 100644 --- a/src/Utils/Exceptions/ValidateException.php +++ b/src/Utils/Exceptions/ValidateException.php @@ -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 );