乐趣区

Result对象 + 统一异常处理

1. 错误异常码设计
1.1 统一异常码接口定义
/**
* 统一异常码接口定义
*
* @author 王洪玉
* @date 2018/11/11
*/

public interface ExceptionEnum {

/**
* 获取异常编码
*
* @return 异常码
*/
Integer getCode();

/**
* 获取异常信息
*
* @return 异常信息
*/
String getMessage();
}
1.2. 通用异常错误码 Enum
/**
* 全局异常错误码
*/
public enum ResultMsgEnum implements ExceptionEnum {

// 请求成功
SUCCESS(200,” 成功 ”),
// 服务器内部错误
ERROR(500,” 失败 ”);

private int code;
private String message;

ResultMsgEnum(int value, String text) {
this.code = value;
this.message = text;
}

@Override
public Integer getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}
}

1.3. 业务异常错误码 Enum
/**
* CMS 系统错误异常码 5001**
*/
public enum CmsErrorCodeEnum implements ExceptionEnum {
// 文章错误
Article_NOT_EXIST(500100,” 该文章不存在 ”);

CmsErrorCodeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}

private Integer code;
private String message;

@Override
public Integer getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}
}
2. Result 对象设计
/**
* 通用返回 result
*/
@Data
public class ZingResult<T> implements Serializable {

private int code;
private String msg;
private T data;

private ZingResult() {
this.code = ResultMsgEnum.SUCCESS.getCode();
this.msg = ResultMsgEnum.SUCCESS.getMessage();
}

private ZingResult(T data) {
this.code = ResultMsgEnum.SUCCESS.getCode();
this.msg = ResultMsgEnum.SUCCESS.getMessage();
this.data = data;
}

private ZingResult(ExceptionEnum exceptionEnum) {
this.code = exceptionEnum.getCode();
this.msg = exceptionEnum.getMessage();
}

public static ZingResult success() {
return new ZingResult();
}

public static <T> ZingResult<T> success(T data) {
return new ZingResult<>(data);
}

public static <T> ZingResult<T> error(ExceptionEnum exceptionEnum) {
return new ZingResult<>(exceptionEnum);
}

}
3. 统一异常处理
3.1 异常父类
/**
* 异常父类
*
* @author 王洪玉
* @date 2018/11/11
*/

@Getter
public class ZingException extends RuntimeException {

private Integer code;
private String message;

public ZingException(ExceptionEnum exceptionEnum){
super(exceptionEnum.getMessage());
this.code = exceptionEnum.getCode();
this.message = exceptionEnum.getMessage();
}
}
3.2 业务异常类
/**
* 业务自定义异常类
*/
@Getter
public class BusinessException extends ZingException {
private ExceptionEnum exceptionEnum;

public BusinessException(ExceptionEnum exceptionEnum) {
super(exceptionEnum);
this.exceptionEnum = exceptionEnum;
}
}
3.3 异常拦截处理

@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {

/**
* 拦截未知错误异常
*
* @param request 请求
* @param e 未知异常
* @return 通用返回格式
*/
@ExceptionHandler(Exception.class)
public ZingResult cmsException(HttpServletRequest request, Exception e) {
log.error(“ 请求的 url 为 {} 出现系统异常, 异常信息为:”, request.getRequestURI(), e);
return ZingResult.error(ResultMsgEnum.ERROR);
}

/**
* 拦截 CMS 业务异常
*
* @param request 请求
* @param e 业务异常
* @return 通用返回格式
*/
@ExceptionHandler(CmsBusinessException.class)
public ZingResult cmsBusinessException(HttpServletRequest request, CmsBusinessException e) {
log.error(“ 请求的 url 为 {} 出现业务异常, 异常信息为:”, request.getRequestURI(), e);
return ZingResult.error(e.getExceptionEnum());
}
}

4. 使用示例
if(CollectionUtils.isEmpty(articleList)){
throw new CmsBusinessException(CmsErrorCodeEnum.ARTICLE_NOT_EXIST);
}

退出移动版