重构错误码的设计,第一次提交~

pull/3/head
YunaiV 2020-07-18 20:12:52 +08:00
parent c5596919f5
commit 4397cbe643
16 changed files with 149 additions and 119 deletions

View File

@ -1,44 +0,0 @@
package cn.iocoder.common.framework.enums;
/**
* Mall
*/
public interface MallConstants {
// 全局请求路径枚举类,用于定义不同用户类型的根请求路径
/**
* -
*/
String ROOT_PATH_USER = "/users";
/**
* -
*/
String ROOT_PATH_ADMIN = "/admins";
// 用户类型
/**
* -
*/
@Deprecated
Integer USER_TYPE_USER = 1;
/**
* -
*/
@Deprecated
Integer USER_TYPE_ADMIN = 2;
// HTTP Request Attr
/**
* HTTP Request Attr -
*/
String REQUEST_ATTR_USER_ID_KEY = "mall_user_id";
/**
* HTTP Request Attr -
*/
String REQUEST_ATTR_USER_TYPE_KEY = "mall_user_type";
/**
* HTTP Request Attr - Controller
*/
String REQUEST_ATTR_COMMON_RESULT = "mall_common_result";
}

View File

@ -1,35 +0,0 @@
package cn.iocoder.common.framework.enums;
/**
*
*
* 使
*
*
*
*
* [0-000-000-000] 1 2 使
*
* 使 1-001-000-000
*
* @author Sin
* @time 2019-03-23 11:28
*/
public class ModuleErrorCodeInterval {
// order 错误码区间 [1-000-001-000 ~ 1-000-002-000]
// user 错误码区间 [1-001-000-000 ~ 1-002-000-000)
// admin 错误码区间 [1-002-000-000 ~ 1-003-000-000)
// product 错误码区间 [1-003-000-000 ~ 1-004-000-000)
// pay 错误码区间 [1-004-000-000 ~ 1-005-000-000)
// cart 错误码区间 [1-005-000-000 ~ 1-006-000-000)
// promotion 错误码区间 [1-006-000-000 ~ 1-007-000-000)
}

View File

@ -0,0 +1,37 @@
package cn.iocoder.common.framework.exception;
import cn.iocoder.common.framework.exception.enums.ServiceErrorCodeRange;
/**
*
*
* [0, 999] {@link GlobalException}
* [1 000 000 000, +) {@link ServiceErrorCodeRange}
*
* TODO i18
*/
public class ErrorCode {
/**
*
*/
private final Integer code;
/**
*
*/
private final String message;
public ErrorCode(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.common.framework.exception;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.vo.CommonResult;
/**
* Exception
*/
public class GlobalException extends RuntimeException {
/**
*
*
* @see GlobalErrorCodeEnum
*/
private final Integer code;
/**
*
* * {@link CommonResult#getDetailMessage()}
*/
private String detailMessage;
public GlobalException(Integer code, String message) {
super(message);
this.code = code;
}
public Integer getCode() {
return code;
}
public String getDetailMessage() {
return detailMessage;
}
public GlobalException setDetailMessage(String detailMessage) {
this.detailMessage = detailMessage;
return this;
}
}

View File

@ -1,9 +1,10 @@
package cn.iocoder.common.framework.exception;
import cn.iocoder.common.framework.exception.enums.ServiceErrorCodeRange;
import cn.iocoder.common.framework.vo.CommonResult;
/**
*
* Exception
*
* https://www.kancloud.cn/onebase/ob/484204 文章
*
@ -32,7 +33,9 @@ import cn.iocoder.common.framework.vo.CommonResult;
public final class ServiceException extends RuntimeException {
/**
*
*
*
* @see ServiceErrorCodeRange
*/
private final Integer code;
/**

View File

@ -1,10 +1,10 @@
package cn.iocoder.common.framework.enums;
package cn.iocoder.common.framework.exception.enums;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
/**
*
* 1-999
* 0-999
*
* {@link GlobalErrorCodeEnum#getCode()} ()} 使 HTTP https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status
* HTTP 使使

View File

@ -0,0 +1,47 @@
package cn.iocoder.common.framework.exception.enums;
/**
* 使
*
* 10
*
* 1
* 1 -
* x -
* 3
* 001 -
* 002 -
* 003 -
* 004 -
* 005 -
* ... - ...
* 3
*
*
* 001 - OAuth2
* 002 - User
* 003 - MobileCode
* 3
*
*
*
* @author Sin
* @time 2019-03-23 11:28
*/
public class ServiceErrorCodeRange {
// order 错误码区间 [1-000-001-000 ~ 1-000-002-000]
// user 错误码区间 [1-001-000-000 ~ 1-002-000-000)
// system-service 服务 => 错误码区间 [1-002-000-000 ~ 1-003-000-000)
// product 错误码区间 [1-003-000-000 ~ 1-004-000-000)
// pay 错误码区间 [1-004-000-000 ~ 1-005-000-000)
// cart 错误码区间 [1-005-000-000 ~ 1-006-000-000)
// promotion 错误码区间 [1-006-000-000 ~ 1-007-000-000)
}

View File

@ -1,40 +1,11 @@
package cn.iocoder.common.framework.util;
import cn.iocoder.common.framework.enums.MallConstants;
import cn.iocoder.common.framework.vo.CommonResult;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import javax.servlet.ServletRequest;
import java.util.UUID;
public class MallUtils {
public static Integer getUserId(ServletRequest request) {
return (Integer) request.getAttribute(MallConstants.REQUEST_ATTR_USER_ID_KEY);
}
public static void setUserId(ServletRequest request, Integer userId) {
request.setAttribute(MallConstants.REQUEST_ATTR_USER_ID_KEY, userId);
}
public static Integer getUserType(ServletRequest request) {
return (Integer) request.getAttribute(MallConstants.REQUEST_ATTR_USER_TYPE_KEY);
}
public static void setUserType(ServletRequest request, Integer userType) {
request.setAttribute(MallConstants.REQUEST_ATTR_USER_TYPE_KEY, userType);
}
@Deprecated
public static CommonResult getCommonResult(ServletRequest request) {
return (CommonResult) request.getAttribute(MallConstants.REQUEST_ATTR_COMMON_RESULT);
}
@Deprecated
public static void setCommonResult(ServletRequest request, CommonResult result) {
request.setAttribute(MallConstants.REQUEST_ATTR_COMMON_RESULT, result);
}
/**
*
*

View File

@ -52,10 +52,13 @@ public class ServiceExceptionUtil {
public static void put(Integer code, String message) {
ServiceExceptionUtil.messages.put(code, message);
}
public static void delete(Integer code, String message) {
ServiceExceptionUtil.messages.remove(code, message);
}
// ========== 和 CommonResult 的集成 ==========
public static <T> CommonResult<T> error(Enumerable enumerable) {
return error(enumerable.getCode());
}
@ -73,6 +76,8 @@ public class ServiceExceptionUtil {
return CommonResult.error(code, message);
}
// ========== 和 ServiceException 的集成 ==========
public static ServiceException exception(Enumerable enumerable) {
String messagePattern = messages.getOrDefault(enumerable.getCode(), enumerable.getMessage());
return exception0(enumerable.getCode(), messagePattern);

View File

@ -1,6 +1,7 @@
package cn.iocoder.common.framework.vo;
import cn.iocoder.common.framework.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.ErrorCode;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.util.Assert;
@ -18,6 +19,8 @@ public final class CommonResult<T> implements Serializable {
/**
*
*
* @see ErrorCode#getCode()
*/
private Integer code;
/**
@ -26,6 +29,8 @@ public final class CommonResult<T> implements Serializable {
private T data;
/**
*
*
* @see ErrorCode#getMsg()
*/
private String message;
/**

View File

@ -1,6 +1,6 @@
package cn.iocoder.mall.dubbo.core.filter;
import cn.iocoder.common.framework.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.util.ExceptionUtil;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;

View File

@ -1,6 +1,6 @@
package cn.iocoder.mall.security.admin.core.interceptor;
import cn.iocoder.common.framework.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.enums.UserTypeEnum;
import cn.iocoder.common.framework.util.CollectionUtils;
import cn.iocoder.common.framework.util.HttpUtil;

View File

@ -1,6 +1,6 @@
package cn.iocoder.mall.security.user.core.interceptor;
import cn.iocoder.common.framework.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.enums.UserTypeEnum;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;

View File

@ -1,6 +1,6 @@
package cn.iocoder.mall.web.core.handler;
import cn.iocoder.common.framework.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.util.ExceptionUtil;
import cn.iocoder.common.framework.util.HttpUtil;

View File

@ -18,7 +18,7 @@ import java.util.Collections;
import java.util.Map;
import java.util.Set;
import static cn.iocoder.common.framework.enums.GlobalErrorCodeEnum.FORBIDDEN;
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum.FORBIDDEN;
/**
* Manager

View File

@ -21,7 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.common.framework.enums.GlobalErrorCodeEnum.FORBIDDEN;
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum.FORBIDDEN;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
/**