【validator】更新异常提示

pull/3/head
fengshuonan 2020-12-26 22:13:27 +08:00
parent 26f692459b
commit 60652fdf04
5 changed files with 40 additions and 35 deletions

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.validator.exception; package cn.stylefeng.roses.kernel.validator.exception;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum; import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.validator.constants.ValidatorConstants; import cn.stylefeng.roses.kernel.validator.constants.ValidatorConstants;
@ -12,8 +13,8 @@ import cn.stylefeng.roses.kernel.validator.constants.ValidatorConstants;
*/ */
public class ParamValidateException extends ServiceException { public class ParamValidateException extends ServiceException {
public ParamValidateException(AbstractExceptionEnum exception, String userTip) { public ParamValidateException(AbstractExceptionEnum exception, Object... params) {
super(ValidatorConstants.VALIDATOR_MODULE_NAME, exception.getErrorCode(), userTip); super(ValidatorConstants.VALIDATOR_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
} }
public ParamValidateException(AbstractExceptionEnum exception) { public ParamValidateException(AbstractExceptionEnum exception) {

View File

@ -15,19 +15,41 @@ import lombok.Getter;
public enum ValidatorExceptionEnum implements AbstractExceptionEnum { public enum ValidatorExceptionEnum implements AbstractExceptionEnum {
/** /**
* * Parameter
*/ */
PARAM_VALIDATE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "01", "参数校验失败,请检查参数的传值是否正确,具体信息{}"), MISSING_SERVLET_REQUEST_PARAMETER_EXCEPTION(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "01", "Parameter传参请求参数缺失异常参数名{},类型为{}"),
/** /**
* * httpMessageConverter
*/ */
INTERRUPT_EXECUTION(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "02", "满足自定义策略要求,程序已中断执行!"), HTTP_MESSAGE_CONVERTER_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "02", "请求Json数据格式错误或Json字段格式转化问题"),
/**
*
*/
HTTP_MEDIA_TYPE_NOT_SUPPORT(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "03", "请求的http media type不合法"),
/**
* http
*/
HTTP_METHOD_NOT_SUPPORT(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "04", "当前接口不支持{}方式请求"),
/**
* 404
*/
NOT_FOUND(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "05", "404找不到请求的资源"),
/**
*
* <p>
* @Valid@Validated
*/
VALIDATED_RESULT_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "06", "参数校验失败,请检查参数的传值是否正确,具体信息:{}"),
/** /**
* *
*/ */
TABLE_UNIQUE_VALIDATE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "03", "数据库字段值唯一性校验出错,参数不完整,字段存在空值:{}"); TABLE_UNIQUE_VALIDATE_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "07", "数据库字段值唯一性校验出错,具体信息{}");
/** /**
* *

View File

@ -63,7 +63,7 @@ public class ValidatorUtil {
public static void validateThrowMessage(Object object, Class<?>... groups) { public static void validateThrowMessage(Object object, Class<?>... groups) {
String errorMessage = validateGetMessage(object, groups); String errorMessage = validateGetMessage(object, groups);
if (errorMessage != null) { if (errorMessage != null) {
throw new ParamValidateException(ValidatorExceptionEnum.PARAM_VALIDATE_ERROR, errorMessage); throw new ParamValidateException(ValidatorExceptionEnum.VALIDATED_RESULT_ERROR, errorMessage);
} }
} }
@ -88,7 +88,7 @@ public class ValidatorUtil {
errorMessage.append(", "); errorMessage.append(", ");
} }
} }
return StrUtil.format(ValidatorExceptionEnum.PARAM_VALIDATE_ERROR.getUserTip(), errorMessage); return StrUtil.format(ValidatorExceptionEnum.VALIDATED_RESULT_ERROR.getUserTip(), errorMessage);
} }
return null; return null;
} }

View File

@ -4,10 +4,9 @@ import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.DbOperatorApi; import cn.stylefeng.roses.kernel.db.api.DbOperatorApi;
import cn.stylefeng.roses.kernel.db.api.context.DbOperatorContext; import cn.stylefeng.roses.kernel.db.api.context.DbOperatorContext;
import cn.stylefeng.roses.kernel.validator.exception.ParamValidateException; import cn.stylefeng.roses.kernel.validator.exception.ParamValidateException;
import cn.stylefeng.roses.kernel.validator.exception.enums.ValidatorExceptionEnum;
import cn.stylefeng.roses.kernel.validator.pojo.UniqueValidateParam; import cn.stylefeng.roses.kernel.validator.pojo.UniqueValidateParam;
import static cn.stylefeng.roses.kernel.validator.exception.enums.ValidatorExceptionEnum.TABLE_UNIQUE_VALIDATE_ERROR;
/** /**
* *
* *
@ -96,16 +95,13 @@ public class TableUniqueValueService {
*/ */
private static void paramValidate(UniqueValidateParam uniqueValidateParam) { private static void paramValidate(UniqueValidateParam uniqueValidateParam) {
if (StrUtil.isBlank(uniqueValidateParam.getTableName())) { if (StrUtil.isBlank(uniqueValidateParam.getTableName())) {
String userTip = StrUtil.format(TABLE_UNIQUE_VALIDATE_ERROR.getUserTip(), "tableName表名"); throw new ParamValidateException(ValidatorExceptionEnum.TABLE_UNIQUE_VALIDATE_ERROR, "@TableUniqueValue注解上tableName属性为空");
throw new ParamValidateException(TABLE_UNIQUE_VALIDATE_ERROR, userTip);
} }
if (StrUtil.isBlank(uniqueValidateParam.getColumnName())) { if (StrUtil.isBlank(uniqueValidateParam.getColumnName())) {
String userTip = StrUtil.format(TABLE_UNIQUE_VALIDATE_ERROR.getUserTip(), "columnName字段名"); throw new ParamValidateException(ValidatorExceptionEnum.TABLE_UNIQUE_VALIDATE_ERROR, "@TableUniqueValue注解上columnName属性为空");
throw new ParamValidateException(TABLE_UNIQUE_VALIDATE_ERROR, userTip);
} }
if (StrUtil.isBlank(uniqueValidateParam.getValue())) { if (StrUtil.isBlank(uniqueValidateParam.getValue())) {
String userTip = StrUtil.format(TABLE_UNIQUE_VALIDATE_ERROR.getUserTip(), "字段值"); throw new ParamValidateException(ValidatorExceptionEnum.TABLE_UNIQUE_VALIDATE_ERROR, "@TableUniqueValue被校验属性的值为空");
throw new ParamValidateException(TABLE_UNIQUE_VALIDATE_ERROR, userTip);
} }
} }
@ -117,8 +113,7 @@ public class TableUniqueValueService {
*/ */
private static void paramIdValidate(UniqueValidateParam uniqueValidateParam) { private static void paramIdValidate(UniqueValidateParam uniqueValidateParam) {
if (uniqueValidateParam.getId() == null) { if (uniqueValidateParam.getId() == null) {
String userTip = StrUtil.format(TABLE_UNIQUE_VALIDATE_ERROR.getUserTip(), "id为空"); throw new ParamValidateException(ValidatorExceptionEnum.TABLE_UNIQUE_VALIDATE_ERROR, StrUtil.toCamelCase(uniqueValidateParam.getIdFieldName()) + "参数值为空");
throw new ParamValidateException(TABLE_UNIQUE_VALIDATE_ERROR, userTip);
} }
} }

View File

@ -1,14 +1,12 @@
package cn.stylefeng.roses.kernel.validator.starter.web; package cn.stylefeng.roses.kernel.validator.starter.web;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.validator.context.RequestParamContext; import cn.stylefeng.roses.kernel.validator.context.RequestParamContext;
import cn.stylefeng.roses.kernel.validator.exception.ParamValidateException;
import org.springframework.core.Conventions; import org.springframework.core.Conventions;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
@ -17,8 +15,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBody
import java.util.List; import java.util.List;
import static cn.stylefeng.roses.kernel.validator.exception.enums.ValidatorExceptionEnum.PARAM_VALIDATE_ERROR;
/** /**
* RequestResponseBodyMethodProcessor * RequestResponseBodyMethodProcessor
* *
@ -47,17 +43,7 @@ public class ValidatorRequestResponseBodyMethodProcessor extends RequestResponse
if (arg != null) { if (arg != null) {
validateIfApplicable(binder, parameter); validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) { if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
List<ObjectError> allErrors = binder.getBindingResult().getAllErrors(); throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
StringBuilder errTips = new StringBuilder();
int index = 1;
for (ObjectError error : allErrors) {
errTips.append(index++);
errTips.append(".");
errTips.append(error.getDefaultMessage());
errTips.append(";");
}
String userTip = StrUtil.format(PARAM_VALIDATE_ERROR.getUserTip(), errTips.toString());
throw new ParamValidateException(PARAM_VALIDATE_ERROR, userTip);
} }
} }
if (mavContainer != null) { if (mavContainer != null) {
@ -67,4 +53,5 @@ public class ValidatorRequestResponseBodyMethodProcessor extends RequestResponse
return adaptArgumentIfNecessary(arg, parameter); return adaptArgumentIfNecessary(arg, parameter);
} }
} }