|
@@ -71,17 +71,17 @@ public class GlobalExceptionHandler {
|
|
|
apiCode = GatewayApiCode.CMS_USER_NO_PERMISSION;
|
|
apiCode = GatewayApiCode.CMS_USER_NO_PERMISSION;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (e instanceof org.springframework.web.bind.MissingServletRequestParameterException) {
|
|
|
|
|
|
|
+ if (isManageRequest() && e instanceof org.springframework.web.bind.MissingServletRequestParameterException) {
|
|
|
message = "缺少必填参数:" + ((org.springframework.web.bind.MissingServletRequestParameterException) e).getParameterName();
|
|
message = "缺少必填参数:" + ((org.springframework.web.bind.MissingServletRequestParameterException) e).getParameterName();
|
|
|
- } else if (e instanceof org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) {
|
|
|
|
|
|
|
+ } else if (isManageRequest() && e instanceof org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) {
|
|
|
message = "参数格式不正确:" + ((org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) e).getName();
|
|
message = "参数格式不正确:" + ((org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) e).getName();
|
|
|
- } else if (e instanceof org.springframework.http.converter.HttpMessageNotReadableException) {
|
|
|
|
|
|
|
+ } else if (isManageRequest() && e instanceof org.springframework.http.converter.HttpMessageNotReadableException) {
|
|
|
message = "请求体格式不正确,请检查 JSON 和字段类型";
|
|
message = "请求体格式不正确,请检查 JSON 和字段类型";
|
|
|
}
|
|
}
|
|
|
if (apiCode != null) {
|
|
if (apiCode != null) {
|
|
|
return GatewayResponse.FAIL.newBuilder().buildGatewayCode(apiCode).toResult();
|
|
return GatewayResponse.FAIL.newBuilder().buildGatewayCode(apiCode).toResult();
|
|
|
}
|
|
}
|
|
|
- return GatewayResponse.FAIL.newBuilder().setMsg(message).toResult("requestId=" + requestId);
|
|
|
|
|
|
|
+ return GatewayResponse.FAIL.newBuilder().setMsg(manageMessage(message, requestId)).toResult("requestId=" + requestId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(value = ConstraintViolationException.class)
|
|
@ExceptionHandler(value = ConstraintViolationException.class)
|
|
@@ -92,9 +92,8 @@ public class GlobalExceptionHandler {
|
|
|
Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
|
|
Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
|
|
|
if (!violations.isEmpty()) {
|
|
if (!violations.isEmpty()) {
|
|
|
for (ConstraintViolation<?> item : violations) {
|
|
for (ConstraintViolation<?> item : violations) {
|
|
|
- System.out.println(item.getPropertyPath());
|
|
|
|
|
// 遍历错误字段信息
|
|
// 遍历错误字段信息
|
|
|
- errorInfoSet.add(item.getMessage());
|
|
|
|
|
|
|
+ errorInfoSet.add(isManageRequest() ? item.getPropertyPath() + ":" + item.getMessage() : item.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
StringBuilder sbf = new StringBuilder();
|
|
StringBuilder sbf = new StringBuilder();
|
|
@@ -107,7 +106,7 @@ public class GlobalExceptionHandler {
|
|
|
|
|
|
|
|
log.error("错误字段 信息: \n Value: {}", message);
|
|
log.error("错误字段 信息: \n Value: {}", message);
|
|
|
|
|
|
|
|
- return GatewayResponse.FAIL.newBuilder().buildGatewayCode(GatewayApiCode.PARAMETER_MISSING_ERROR).setMsg(message).toResult();
|
|
|
|
|
|
|
+ return validationFailure(message);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@ExceptionHandler(BindException.class)
|
|
@ExceptionHandler(BindException.class)
|
|
@@ -122,7 +121,7 @@ public class GlobalExceptionHandler {
|
|
|
Set<String> errorInfoSet = new TreeSet<String>();
|
|
Set<String> errorInfoSet = new TreeSet<String>();
|
|
|
for (FieldError fieldError : fieldErrorList) {
|
|
for (FieldError fieldError : fieldErrorList) {
|
|
|
// 遍历错误字段信息
|
|
// 遍历错误字段信息
|
|
|
- errorInfoSet.add(fieldError.getField() + ":" + (fieldError.isBindingFailure() ? "格式或类型不正确" : fieldError.getDefaultMessage()));
|
|
|
|
|
|
|
+ errorInfoSet.add(isManageRequest() ? fieldError.getField() + ":" + (fieldError.isBindingFailure() ? "格式或类型不正确" : fieldError.getDefaultMessage()) : fieldError.getDefaultMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
StringBuilder sbf = new StringBuilder();
|
|
StringBuilder sbf = new StringBuilder();
|
|
@@ -130,13 +129,38 @@ public class GlobalExceptionHandler {
|
|
|
sbf.append(errorInfo);
|
|
sbf.append(errorInfo);
|
|
|
sbf.append(",");
|
|
sbf.append(",");
|
|
|
}
|
|
}
|
|
|
- String message = sbf.substring(0, sbf.length() - 1);
|
|
|
|
|
|
|
+ String message = sbf.length() == 0 ? "参数校验失败" : sbf.substring(0, sbf.length() - 1);
|
|
|
|
|
|
|
|
log.error("验证参数信息是否有效 错误信息: \n Value: {}", message);
|
|
log.error("验证参数信息是否有效 错误信息: \n Value: {}", message);
|
|
|
|
|
|
|
|
- return GatewayResponse.FAIL.newBuilder().buildGatewayCode(GatewayApiCode.PARAMETER_MISSING_ERROR).setMsg(message).toResult();
|
|
|
|
|
|
|
+ return validationFailure(message);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
return GatewayResponse.SUCCESS.newBuilder().buildGatewayCode(GatewayApiCode.SUCCESS).setMsg("参数校验通过").toResult();
|
|
return GatewayResponse.SUCCESS.newBuilder().buildGatewayCode(GatewayApiCode.SUCCESS).setMsg("参数校验通过").toResult();
|
|
|
}
|
|
}
|
|
|
|
|
+ private boolean isManageRequest() {
|
|
|
|
|
+ String path = request.getRequestURI();
|
|
|
|
|
+ String context = request.getContextPath();
|
|
|
|
|
+ if (context != null && !context.isEmpty() && path.startsWith(context + "/")) {
|
|
|
|
|
+ path = path.substring(context.length());
|
|
|
|
|
+ }
|
|
|
|
|
+ // Do not trust client-supplied forwarded path headers.
|
|
|
|
|
+ return "/manage".equals(path) || path.startsWith("/manage/");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String manageMessage(String message, String requestId) {
|
|
|
|
|
+ return isManageRequest() ? message + "(错误编号:" + requestId + ")" : message;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Result<String> validationFailure(String message) {
|
|
|
|
|
+ if (!isManageRequest()) {
|
|
|
|
|
+ return GatewayResponse.FAIL.newBuilder().buildGatewayCode(GatewayApiCode.PARAMETER_MISSING_ERROR)
|
|
|
|
|
+ .setMsg(message).toResult();
|
|
|
|
|
+ }
|
|
|
|
|
+ String requestId = java.util.UUID.randomUUID().toString();
|
|
|
|
|
+ log.warn("Validation failed requestId={} path={} message={}", requestId, request.getRequestURI(), message);
|
|
|
|
|
+ return GatewayResponse.FAIL.newBuilder().buildGatewayCode(GatewayApiCode.PARAMETER_MISSING_ERROR)
|
|
|
|
|
+ .setMsg(manageMessage(message, requestId)).toResult("requestId=" + requestId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|