增加简单的部署打包脚本
增加 Dubbo 参数校验
This commit is contained in:
@@ -3,13 +3,14 @@ package cn.iocoder.common.framework.config;
|
||||
import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
import cn.iocoder.common.framework.util.ExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.RestResult;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
@ControllerAdvice
|
||||
@@ -17,33 +18,49 @@ public class GlobalExceptionHandler {
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = ServiceException.class)
|
||||
public RestResult serviceExceptionHandler(HttpServletRequest req, ServiceException ex) {
|
||||
return RestResult.error(ex.getCode(), ex.getMessage());
|
||||
public CommonResult serviceExceptionHandler(HttpServletRequest req, ServiceException ex) {
|
||||
return CommonResult.error(ex.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = ConstraintViolationException.class)
|
||||
public CommonResult constraintViolationExceptionHandler(HttpServletRequest req, ConstraintViolationException ex) {
|
||||
// TODO 芋艿,后续要想一个更好的方式。
|
||||
// 拼接详细报错
|
||||
StringBuilder detailMessage = new StringBuilder("\n\n详细错误如下:");
|
||||
ex.getConstraintViolations().forEach(constraintViolation -> detailMessage.append("\n").append(constraintViolation.getMessage()));
|
||||
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getMessage()
|
||||
+ detailMessage.toString());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = UndeclaredThrowableException.class)
|
||||
public RestResult undeclaredThrowableExceptionHandler(HttpServletRequest req, UndeclaredThrowableException e) {
|
||||
public CommonResult undeclaredThrowableExceptionHandler(HttpServletRequest req, UndeclaredThrowableException e) {
|
||||
// 尝试获得 ServiceException 异常。如果是,则使用 serviceExceptionHandler 方法处理。
|
||||
ServiceException serviceException = ExceptionUtil.getServiceException(e);
|
||||
if (serviceException != null) {
|
||||
return serviceExceptionHandler(req, serviceException);
|
||||
}
|
||||
// 尝试获得 ConstraintViolationException 异常。如果是,
|
||||
ConstraintViolationException constraintViolationException = ExceptionUtil.getConstraintViolationException(e);
|
||||
if (constraintViolationException != null) {
|
||||
return constraintViolationExceptionHandler(req, constraintViolationException);
|
||||
}
|
||||
// 获得不到,使用 异常日志 方法处理。
|
||||
return resultExceptionHandler(req, e);
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public RestResult resultExceptionHandler(HttpServletRequest req, Exception e) {
|
||||
public CommonResult resultExceptionHandler(HttpServletRequest req, Exception e) {
|
||||
// TODO 异常日志
|
||||
e.printStackTrace();
|
||||
// TODO 翻译不同的异常
|
||||
if (e instanceof MissingServletRequestParameterException) {
|
||||
return RestResult.error(SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getCode(), SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getMessage());
|
||||
return CommonResult.error(SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getCode(), SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getMessage());
|
||||
}
|
||||
// 返回
|
||||
return RestResult.error(SysErrorCodeEnum.SYS_ERROR.getCode(), SysErrorCodeEnum.SYS_ERROR.getMessage());
|
||||
return CommonResult.error(SysErrorCodeEnum.SYS_ERROR.getCode(), SysErrorCodeEnum.SYS_ERROR.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@ public enum SysErrorCodeEnum {
|
||||
|
||||
SYS_ERROR(2001001000, "服务端发生异常"),
|
||||
MISSING_REQUEST_PARAM_ERROR(2001001001, "参数缺失"),
|
||||
VALIDATION_REQUEST_PARAM_ERROR(2001001002, "参数校验不正确")
|
||||
;
|
||||
|
||||
private final int code;
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.iocoder.common.framework.util;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
@@ -31,4 +32,16 @@ public class ExceptionUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ConstraintViolationException getConstraintViolationException(UndeclaredThrowableException e) {
|
||||
Throwable undeclaredThrowable = e.getUndeclaredThrowable();
|
||||
if (undeclaredThrowable instanceof InvocationTargetException) {
|
||||
InvocationTargetException invocationTargetException = (InvocationTargetException) undeclaredThrowable;
|
||||
Throwable targetException = invocationTargetException.getTargetException();
|
||||
if (targetException != null && targetException instanceof ConstraintViolationException) {
|
||||
return (ConstraintViolationException) targetException;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user