Blame view

src/main/java/cn/iocoder/dashboard/common/exception/util/ServiceExceptionUtil.java 4.57 KB
0a607861   YunaiV   common 包,基础组件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  package cn.iocoder.dashboard.common.exception.util;
  
  import cn.iocoder.dashboard.common.exception.ErrorCode;
  import cn.iocoder.dashboard.common.exception.ServiceException;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import java.util.Map;
  import java.util.concurrent.ConcurrentHashMap;
  import java.util.concurrent.ConcurrentMap;
  
  /**
   * {@link ServiceException} 工具类
   *
   * 目的在于,格式化异常信息提示。
   * 考虑到 String.format 在参数不正确时会报错,因此使用 {} 作为占位符,并使用 {@link #doFormat(int, String, Object...)} 方法来格式化
   *
   * 因为 {@link #MESSAGES} 里面默认是没有异常信息提示的模板的,所以需要使用方自己初始化进去。目前想到的有几种方式:
   *
   * 1. 异常提示信息,写在枚举类中,例如说,cn.iocoder.oceans.user.api.constants.ErrorCodeEnum 类 + ServiceExceptionConfiguration
   * 2. 异常提示信息,写在 .properties 等等配置文件
   * 3. 异常提示信息,写在 Apollo 等等配置中心中,从而实现可动态刷新
   * 4. 异常提示信息,存储在 db 等等数据库中,从而实现可动态刷新
   */
  public class ServiceExceptionUtil {
  
      private static final Logger LOGGER = LoggerFactory.getLogger(ServiceExceptionUtil.class);
  
      /**
       * 错误码提示模板
       */
      private static final ConcurrentMap<Integer, String> MESSAGES = new ConcurrentHashMap<>();
  
      public static void putAll(Map<Integer, String> messages) {
          ServiceExceptionUtil.MESSAGES.putAll(messages);
      }
  
      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);
      }
  
      // ========== 和 ServiceException 的集成 ==========
  
      public static ServiceException exception(ErrorCode errorCode) {
          String messagePattern = MESSAGES.getOrDefault(errorCode.getCode(), errorCode.getMessage());
          return exception0(errorCode.getCode(), messagePattern);
      }
  
      public static ServiceException exception(ErrorCode errorCode, Object... params) {
          String messagePattern = MESSAGES.getOrDefault(errorCode.getCode(), errorCode.getMessage());
          return exception0(errorCode.getCode(), messagePattern, params);
      }
  
      /**
       * 创建指定编号的 ServiceException 的异常
       *
       * @param code 编号
       * @return 异常
       */
      public static ServiceException exception(Integer code) {
          return exception0(code, MESSAGES.get(code));
      }
  
      /**
       * 创建指定编号的 ServiceException 的异常
       *
       * @param code 编号
       * @param params 消息提示的占位符对应的参数
       * @return 异常
       */
      public static ServiceException exception(Integer code, Object... params) {
          return exception0(code, MESSAGES.get(code), params);
      }
  
      public static ServiceException exception0(Integer code, String messagePattern, Object... params) {
          String message = doFormat(code, messagePattern, params);
          return new ServiceException(code, message);
      }
  
      // ========== 格式化方法 ==========
  
      /**
       * 将错误编号对应的消息使用 params 进行格式化。
       *
       * @param code           错误编号
       * @param messagePattern 消息模版
       * @param params         参数
       * @return 格式化后的提示
       */
      private static String doFormat(int code, String messagePattern, Object... params) {
          StringBuilder sbuf = new StringBuilder(messagePattern.length() + 50);
          int i = 0;
          int j;
          int l;
          for (l = 0; l < params.length; l++) {
              j = messagePattern.indexOf("{}", i);
              if (j == -1) {
                  LOGGER.error("[doFormat][参数过多:错误码({})|错误内容({})|参数({})", code, messagePattern, params);
                  if (i == 0) {
                      return messagePattern;
                  } else {
                      sbuf.append(messagePattern.substring(i, messagePattern.length()));
                      return sbuf.toString();
                  }
              } else {
                  sbuf.append(messagePattern.substring(i, j));
                  sbuf.append(params[l]);
                  i = j + 2;
              }
          }
          if (messagePattern.indexOf("{}", i) != -1) {
              LOGGER.error("[doFormat][参数过少:错误码({})|错误内容({})|参数({})", code, messagePattern, params);
          }
          sbuf.append(messagePattern.substring(i, messagePattern.length()));
          return sbuf.toString();
      }
  
  }