ServiceExceptionUtil.java 4.5 KB

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