ServiceExceptionUtil.java 4.6 KB

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