- 前端:完善商品列表
- 后端:将商品模块的 Service 改成有业务异常时,抛出异常,而不是返回 CommonResult - 后端:将搜索模块的 Service 改成有业务异常时,抛出异常,而不是返回 CommonResult
This commit is contained in:
@@ -2,8 +2,9 @@ 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.CommonResult;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
@@ -11,20 +12,32 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
// 逻辑异常
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = ServiceException.class)
|
||||
public CommonResult serviceExceptionHandler(HttpServletRequest req, ServiceException ex) {
|
||||
logger.debug("[serviceExceptionHandler]", ex);
|
||||
return CommonResult.error(ex.getCode(), ex.getMessage());
|
||||
}
|
||||
|
||||
// Spring MVC 参数不正确
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = MissingServletRequestParameterException.class)
|
||||
public CommonResult missingServletRequestParameterExceptionHandler(HttpServletRequest req, MissingServletRequestParameterException ex) {
|
||||
logger.warn("[missingServletRequestParameterExceptionHandler]", ex);
|
||||
return CommonResult.error(SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getCode(), SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getMessage() + ":" + ex.getMessage());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = ConstraintViolationException.class)
|
||||
public CommonResult constraintViolationExceptionHandler(HttpServletRequest req, ConstraintViolationException ex) {
|
||||
logger.info("[constraintViolationExceptionHandler]", ex);
|
||||
// TODO 芋艿,后续要想一个更好的方式。
|
||||
// 拼接详细报错
|
||||
StringBuilder detailMessage = new StringBuilder("\n\n详细错误如下:");
|
||||
@@ -33,34 +46,12 @@ public class GlobalExceptionHandler {
|
||||
+ detailMessage.toString());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = UndeclaredThrowableException.class)
|
||||
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 CommonResult resultExceptionHandler(HttpServletRequest req, Exception e) {
|
||||
// TODO 翻译不同的异常
|
||||
if (e instanceof MissingServletRequestParameterException) {
|
||||
return CommonResult.error(SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getCode(), SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getMessage() + ":" + e.getMessage());
|
||||
}
|
||||
// TODO 异常日志
|
||||
e.printStackTrace();
|
||||
logger.error("[resultExceptionHandler]", e);
|
||||
// 返回
|
||||
return CommonResult.error(SysErrorCodeEnum.SYS_ERROR.getCode(), SysErrorCodeEnum.SYS_ERROR.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package cn.iocoder.common.framework.vo;
|
||||
|
||||
public class RestResult {
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private Integer code;
|
||||
/**
|
||||
* 错误提示
|
||||
*/
|
||||
private String message;
|
||||
/**
|
||||
* 返回数据
|
||||
*/
|
||||
private Object data;
|
||||
|
||||
public static RestResult error(Integer code, String message) {
|
||||
RestResult result = new RestResult();
|
||||
result.code = code;
|
||||
result.message = message;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static RestResult ok(Object data) {
|
||||
RestResult result = new RestResult();
|
||||
result.code = 0;
|
||||
result.data = data;
|
||||
result.message = "";
|
||||
return result;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user