diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/MallConstants.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/MallConstants.java deleted file mode 100644 index a32154ba4..000000000 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/MallConstants.java +++ /dev/null @@ -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"; - -} diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/ModuleErrorCodeInterval.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/ModuleErrorCodeInterval.java deleted file mode 100644 index 457fc0d6e..000000000 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/ModuleErrorCodeInterval.java +++ /dev/null @@ -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) - - -} diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/ErrorCode.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/ErrorCode.java new file mode 100644 index 000000000..c6fdf019b --- /dev/null +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/ErrorCode.java @@ -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; + } + +} diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/GlobalException.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/GlobalException.java new file mode 100644 index 000000000..615007191 --- /dev/null +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/GlobalException.java @@ -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; + } + +} diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/ServiceException.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/ServiceException.java index 267ba934c..e2a72a93c 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/ServiceException.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/ServiceException.java @@ -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; /** diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/GlobalErrorCodeEnum.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/enums/GlobalErrorCodeEnum.java similarity index 93% rename from common/common-framework/src/main/java/cn/iocoder/common/framework/enums/GlobalErrorCodeEnum.java rename to common/common-framework/src/main/java/cn/iocoder/common/framework/exception/enums/GlobalErrorCodeEnum.java index b948cda81..9774c31a8 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/enums/GlobalErrorCodeEnum.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/enums/GlobalErrorCodeEnum.java @@ -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 响应状态码作为业务使用表达能力偏弱,但是使用在系统层面还是非常不错的 diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/enums/ServiceErrorCodeRange.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/enums/ServiceErrorCodeRange.java new file mode 100644 index 000000000..9c759f885 --- /dev/null +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/exception/enums/ServiceErrorCodeRange.java @@ -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) + +} diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/MallUtils.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/MallUtils.java index d28e3e319..1205c5aec 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/MallUtils.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/MallUtils.java @@ -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); - } - /** * 获得链路追踪编号 * diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java index d3b8642d6..e9df095ef 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java @@ -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 CommonResult 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); diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java index 1fb1d0158..c6972ffe9 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/vo/CommonResult.java @@ -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 implements Serializable { /** * 错误码 + * + * @see ErrorCode#getCode() */ private Integer code; /** @@ -26,6 +29,8 @@ public final class CommonResult implements Serializable { private T data; /** * 错误提示,用户可阅读 + * + * @see ErrorCode#getMsg() */ private String message; /** diff --git a/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java b/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java index 7bc5aca09..d2fd83373 100644 --- a/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java +++ b/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java @@ -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; diff --git a/common/mall-spring-boot-starter-security-admin/src/main/java/cn/iocoder/mall/security/admin/core/interceptor/AdminSecurityInterceptor.java b/common/mall-spring-boot-starter-security-admin/src/main/java/cn/iocoder/mall/security/admin/core/interceptor/AdminSecurityInterceptor.java index baff3997b..617ceb76a 100644 --- a/common/mall-spring-boot-starter-security-admin/src/main/java/cn/iocoder/mall/security/admin/core/interceptor/AdminSecurityInterceptor.java +++ b/common/mall-spring-boot-starter-security-admin/src/main/java/cn/iocoder/mall/security/admin/core/interceptor/AdminSecurityInterceptor.java @@ -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; diff --git a/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java index 14033d8bb..a3b1f4af9 100644 --- a/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java +++ b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java @@ -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; diff --git a/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java b/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java index 138030c6f..eec4f6579 100644 --- a/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java +++ b/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java @@ -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; diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/permission/PermissionManager.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/permission/PermissionManager.java index 8e6a8ca96..d4f8b565b 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/permission/PermissionManager.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/manager/permission/PermissionManager.java @@ -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 diff --git a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/permission/PermissionService.java b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/permission/PermissionService.java index 7c1f195d5..d757f9251 100644 --- a/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/permission/PermissionService.java +++ b/system-service-project/system-service-app/src/main/java/cn/iocoder/mall/systemservice/service/permission/PermissionService.java @@ -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.*; /**