system-service 服务,接入新的错误码

This commit is contained in:
YunaiV
2020-07-19 00:41:30 +08:00
parent 4397cbe643
commit 32c1cfb3a7
110 changed files with 381 additions and 475 deletions

View File

@@ -1,6 +1,6 @@
package cn.iocoder.common.framework.exception;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.common.framework.vo.CommonResult;
/**
@@ -11,18 +11,34 @@ public class GlobalException extends RuntimeException {
/**
* 全局错误码
*
* @see GlobalErrorCodeEnum
* @see GlobalErrorCodeConstants
*/
private final Integer code;
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 错误明细,内部调试错误
* * 和 {@link CommonResult#getDetailMessage()} 一致的设计
*
* 和 {@link CommonResult#getDetailMessage()} 一致的设计
*/
private String detailMessage;
/**
* 空构造方法,避免反序列化问题
*/
public GlobalException() {
}
public GlobalException(ErrorCode errorCode) {
this.code = errorCode.getCode();
this.message = errorCode.getMessage();
}
public GlobalException(Integer code, String message) {
super(message);
this.code = code;
this.message = message;
}
public Integer getCode() {
@@ -38,4 +54,18 @@ public class GlobalException extends RuntimeException {
return this;
}
public GlobalException setCode(Integer code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public GlobalException setMessage(String message) {
this.message = message;
return this;
}
}

View File

@@ -5,30 +5,6 @@ import cn.iocoder.common.framework.vo.CommonResult;
/**
* 业务逻辑异常 Exception
*
* 参考 https://www.kancloud.cn/onebase/ob/484204 文章
*
* 一共 10 位,分成四段
*
* 第一段1 位,类型
* 1 - 业务级别异常
* 2 - 系统级别异常
* 第二段3 位,系统类型
* 001 - 用户系统
* 002 - 商品系统
* 003 - 订单系统
* 004 - 支付系统
* 005 - 优惠劵系统
* ... - ...
* 第三段3 位,模块
* 不限制规则。
* 一般建议,每个系统里面,可能有多个模块,可以再去做分段。以用户系统为例子:
* 001 - OAuth2 模块
* 002 - User 模块
* 003 - MobileCode 模块
* 第四段3 位,错误码
* 不限制规则。
* 一般建议,每个模块自增。
*/
public final class ServiceException extends RuntimeException {
@@ -37,7 +13,11 @@ public final class ServiceException extends RuntimeException {
*
* @see ServiceErrorCodeRange
*/
private final Integer code;
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 错误明细,内部调试错误
*
@@ -45,9 +25,20 @@ public final class ServiceException extends RuntimeException {
*/
private String detailMessage;
/**
* 空构造方法,避免反序列化问题
*/
public ServiceException() {
}
public ServiceException(ErrorCode errorCode) {
this.code = errorCode.getCode();
this.message = errorCode.getMessage();
}
public ServiceException(Integer code, String message) {
super(message);
this.code = code;
this.message = message;
}
public Integer getCode() {
@@ -63,4 +54,18 @@ public final class ServiceException extends RuntimeException {
return this;
}
public ServiceException setCode(Integer code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public ServiceException setMessage(String message) {
this.message = message;
return this;
}
}

View File

@@ -0,0 +1,37 @@
package cn.iocoder.common.framework.exception.enums;
import cn.iocoder.common.framework.exception.ErrorCode;
/**
* 全局错误码枚举
* 0-999 系统异常编码保留
*
* 一般情况下,使用 HTTP 响应状态码 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status
* 虽然说HTTP 响应状态码作为业务使用表达能力偏弱,但是使用在系统层面还是非常不错的
* 比较特殊的是,因为之前一直使用 0 作为成功,就不使用 200 啦。
*/
public interface GlobalErrorCodeConstants {
ErrorCode SUCCESS = new ErrorCode(0, "成功");
// ========== 客户端错误段 ==========
ErrorCode BAD_REQUEST = new ErrorCode(400, "请求参数不正确");
ErrorCode UNAUTHORIZED = new ErrorCode(401, "账号未登录");
ErrorCode FORBIDDEN = new ErrorCode(403, "没有该操作权限");
ErrorCode NOT_FOUND = new ErrorCode(404, "请求未找到");
ErrorCode METHOD_NOT_ALLOWED = new ErrorCode(405, "请求方法不正确");
// ========== 服务端错误段 ==========
ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(500, "系统异常");
ErrorCode UNKNOWN = new ErrorCode(999, "未知错误");
static boolean isMatch(Integer code) {
return code != null
&& code >= SUCCESS.getCode() && code <= UNKNOWN.getCode();
}
}

View File

@@ -1,51 +0,0 @@
package cn.iocoder.common.framework.exception.enums;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
/**
* 全局错误码枚举
* 0-999 系统异常编码保留
*
* 一般情况下,{@link GlobalErrorCodeEnum#getCode()} ()} 使用 HTTP 响应状态码 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status
* 虽然说HTTP 响应状态码作为业务使用表达能力偏弱,但是使用在系统层面还是非常不错的
* 比较特殊的是,因为之前一直使用 0 作为成功,就不使用 200 啦。
*/
public enum GlobalErrorCodeEnum implements ServiceExceptionUtil.Enumerable<GlobalErrorCodeEnum> {
SUCCESS(0, "成功"),
// ========== 客户端错误段 ==========
BAD_REQUEST(400, "请求参数不正确"),
UNAUTHORIZED(401, "账号未登录"),
FORBIDDEN(403, "没有该操作权限"),
NOT_FOUND(404, "请求未找到"),
METHOD_NOT_ALLOWED(405, "请求方法不正确"),
// ========== 服务端错误段 ==========
INTERNAL_SERVER_ERROR(500, "系统异常"),
;
private final int code;
private final String message;
GlobalErrorCodeEnum(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
@Override
public int getGroup() {
return 0;
}
}

View File

@@ -1,10 +1,9 @@
package cn.iocoder.common.framework.util;
import cn.iocoder.common.framework.exception.ErrorCode;
import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.vo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -27,19 +26,6 @@ public class ServiceExceptionUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceExceptionUtil.class);
/**
* 错误枚举的接口
*/
public interface Enumerable<V extends Enum> {
int getCode();
String getMessage();
int getGroup();
}
/**
* 错误码提示模板
*/
@@ -57,35 +43,16 @@ public class ServiceExceptionUtil {
ServiceExceptionUtil.messages.remove(code, message);
}
// ========== 和 CommonResult 的集成 ==========
public static <T> CommonResult<T> error(Enumerable enumerable) {
return error(enumerable.getCode());
}
public static <T> CommonResult<T> error(Enumerable enumerable, Object... params) {
return error(enumerable.getCode(), params);
}
public static <T> CommonResult<T> error(Integer code) {
return CommonResult.error(code, messages.get(code));
}
public static <T> CommonResult<T> error(Integer code, Object... params) {
String message = doFormat(code, messages.get(code), params);
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);
public static ServiceException exception(ErrorCode errorCode) {
String messagePattern = messages.getOrDefault(errorCode.getCode(), errorCode.getMessage());
return exception0(errorCode.getCode(), messagePattern);
}
public static ServiceException exception(Enumerable enumerable, Object... params) {
String messagePattern = messages.getOrDefault(enumerable.getCode(), enumerable.getMessage());
return exception0(enumerable.getCode(), messagePattern, params);
public static ServiceException exception(ErrorCode errorCode, Object... params) {
String messagePattern = messages.getOrDefault(errorCode.getCode(), errorCode.getMessage());
return exception0(errorCode.getCode(), messagePattern, params);
}
/**
@@ -114,10 +81,7 @@ public class ServiceExceptionUtil {
return new ServiceException(code, message);
}
public static ServiceException exception(CommonResult result) {
Assert.isTrue(result.isError(), "结果必须是错误的");
return new ServiceException(result.getCode(), result.getMessage());
}
// ========== 格式化方法 ==========
/**
* 将错误编号对应的消息使用 params 进行格式化。

View File

@@ -1,8 +1,9 @@
package cn.iocoder.common.framework.vo;
import cn.iocoder.common.framework.exception.ErrorCode;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.exception.GlobalException;
import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants;
import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.util.Assert;
@@ -15,8 +16,6 @@ import java.io.Serializable;
*/
public final class CommonResult<T> implements Serializable {
private static final Integer CODE_SUCCESS = GlobalErrorCodeEnum.SUCCESS.getCode();
/**
* 错误码
*
@@ -30,7 +29,7 @@ public final class CommonResult<T> implements Serializable {
/**
* 错误提示,用户可阅读
*
* @see ErrorCode#getMsg()
* @see ErrorCode#getMessage() ()
*/
private String message;
/**
@@ -48,20 +47,25 @@ public final class CommonResult<T> implements Serializable {
* @return 新的 CommonResult 对象
*/
public static <T> CommonResult<T> error(CommonResult<?> result) {
return error(result.getCode(), result.getMessage());
return error(result.getCode(), result.getMessage(), result.detailMessage);
}
public static <T> CommonResult<T> error(Integer code, String message) {
Assert.isTrue(!CODE_SUCCESS.equals(code), "code 必须是错误的!");
return error(code, message, null);
}
public static <T> CommonResult<T> error(Integer code, String message, String detailMessage) {
Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
CommonResult<T> result = new CommonResult<>();
result.code = code;
result.message = message;
result.detailMessage = detailMessage;
return result;
}
public static <T> CommonResult<T> success(T data) {
CommonResult<T> result = new CommonResult<>();
result.code = CODE_SUCCESS;
result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
result.data = data;
result.message = "";
return result;
@@ -102,7 +106,7 @@ public final class CommonResult<T> implements Serializable {
@JSONField(serialize = false) // 避免序列化
public boolean isSuccess() {
return CODE_SUCCESS.equals(code);
return GlobalErrorCodeConstants.SUCCESS.getCode().equals(code);
}
@JSONField(serialize = false) // 避免序列化
@@ -110,23 +114,31 @@ public final class CommonResult<T> implements Serializable {
return !isSuccess();
}
/**
* 判断是否有异常。如果有,则抛出 {@link cn.iocoder.common.framework.exception.ServiceException} 异常
*/
public void checkError() {
if (isSuccess()) {
return;
}
throw ServiceExceptionUtil.exception0(code, message);
}
@Override
public String toString() {
return "CommonResult{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
", message='" + message + '\'' +
", detailMessage='" + detailMessage + '\'' +
'}';
}
// ========= 和 Exception 异常体系集成 =========
/**
* 判断是否有异常。如果有,则抛出 {@link GlobalException} 或 {@link ServiceException} 异常
*/
public void checkError() throws GlobalException, ServiceException {
if (isSuccess()) {
return;
}
// 全局异常
if (GlobalErrorCodeConstants.isMatch(code)) {
throw new GlobalException(code, message).setDetailMessage(detailMessage);
}
// 业务异常
throw new ServiceException(code, message).setDetailMessage(detailMessage);
}
}