Commit e5f343a3e991517ae57651cf2c893366503c148f

Authored by YunaiV
1 parent 7a86a610

【重构】新增 `yudao-spring-boot-starter-biz-error-code` 错误码组件

yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/ServerException.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.common.exception;
  2 +
  3 +import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
  4 +import lombok.Data;
  5 +import lombok.EqualsAndHashCode;
  6 +
  7 +/**
  8 + * 服务器异常 Exception
  9 + */
  10 +@Data
  11 +@EqualsAndHashCode(callSuper = true)
  12 +public final class ServerException extends RuntimeException {
  13 +
  14 + /**
  15 + * 全局错误码
  16 + *
  17 + * @see GlobalErrorCodeConstants
  18 + */
  19 + private Integer code;
  20 + /**
  21 + * 错误提示
  22 + */
  23 + private String message;
  24 +
  25 + /**
  26 + * 空构造方法,避免反序列化问题
  27 + */
  28 + public ServerException() {
  29 + }
  30 +
  31 + public ServerException(ErrorCode errorCode) {
  32 + this.code = errorCode.getCode();
  33 + this.message = errorCode.getMsg();
  34 + }
  35 +
  36 + public ServerException(Integer code, String message) {
  37 + this.code = code;
  38 + this.message = message;
  39 + }
  40 +
  41 + public Integer getCode() {
  42 + return code;
  43 + }
  44 +
  45 + public ServerException setCode(Integer code) {
  46 + this.code = code;
  47 + return this;
  48 + }
  49 +
  50 + @Override
  51 + public String getMessage() {
  52 + return message;
  53 + }
  54 +
  55 + public ServerException setMessage(String message) {
  56 + this.message = message;
  57 + return this;
  58 + }
  59 +
  60 +}
... ...
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/enums/GlobalErrorCodeConstants.java
... ... @@ -36,9 +36,15 @@ public interface GlobalErrorCodeConstants {
36 36  
37 37 ErrorCode UNKNOWN = new ErrorCode(999, "未知错误");
38 38  
39   - static boolean isMatch(Integer code) {
  39 + /**
  40 + * 是否为服务端错误,参考 HTTP 5XX 错误码段
  41 + *
  42 + * @param code 错误码
  43 + * @return 是否
  44 + */
  45 + static boolean isServerErrorCode(Integer code) {
40 46 return code != null
41   - && code >= SUCCESS.getCode() && code <= UNKNOWN.getCode();
  47 + && code >= INTERNAL_SERVER_ERROR.getCode() && code <= INTERNAL_SERVER_ERROR.getCode() + 99;
42 48 }
43 49  
44 50 }
... ...
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/pojo/CommonResult.java
1 1 package cn.iocoder.yudao.framework.common.pojo;
2 2  
3 3 import cn.iocoder.yudao.framework.common.exception.ErrorCode;
  4 +import cn.iocoder.yudao.framework.common.exception.ServerException;
4 5 import cn.iocoder.yudao.framework.common.exception.ServiceException;
5 6 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
6 7 import com.fasterxml.jackson.annotation.JsonIgnore;
... ... @@ -91,10 +92,24 @@ public class CommonResult&lt;T&gt; implements Serializable {
91 92 if (isSuccess()) {
92 93 return;
93 94 }
  95 + // 服务端异常
  96 + if (GlobalErrorCodeConstants.isServerErrorCode(code)) {
  97 + throw new ServerException(code, msg);
  98 + }
94 99 // 业务异常
95 100 throw new ServiceException(code, msg);
96 101 }
97 102  
  103 + /**
  104 + * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
  105 + * 如果没有,则返回 {@link #data} 数据
  106 + */
  107 + @JsonIgnore // 避免 jackson 序列化
  108 + public T getCheckedData() {
  109 + checkError();
  110 + return data;
  111 + }
  112 +
98 113 public static <T> CommonResult<T> error(ServiceException serviceException) {
99 114 return error(serviceException.getCode(), serviceException.getMessage());
100 115 }
... ...
yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/config/ErrorCodeConfiguration.java renamed to yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/config/YudaoErrorCodeConfiguration.java
... ... @@ -19,7 +19,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
19 19 @Configuration
20 20 @EnableConfigurationProperties(ErrorCodeProperties.class)
21 21 @EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码
22   -public class ErrorCodeConfiguration {
  22 +public class YudaoErrorCodeConfiguration {
23 23  
24 24 @Bean
25 25 public ErrorCodeAutoGenerator errorCodeAutoGenerator(@Value("${spring.application.name}") String applicationName,
... ...
yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/resources/META-INF/spring.factories
1 1 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2   - cn.iocoder.yudao.framework.errorcode.config.ErrorCodeConfiguration
  2 + cn.iocoder.yudao.framework.errorcode.config.YudaoErrorCodeConfiguration
... ...