BaseException.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.ruoyi.common.exception.base;
  2. import com.ruoyi.common.utils.MessageUtils;
  3. import com.ruoyi.common.utils.StringUtils;
  4. /**
  5. * 基础异常
  6. *
  7. * @author ruoyi
  8. */
  9. public class BaseException extends RuntimeException
  10. {
  11. private static final long serialVersionUID = 1L;
  12. /**
  13. * 所属模块
  14. */
  15. private String module;
  16. /**
  17. * 错误码
  18. */
  19. private String code;
  20. /**
  21. * 错误码对应的参数
  22. */
  23. private Object[] args;
  24. /**
  25. * 错误消息
  26. */
  27. private String defaultMessage;
  28. public BaseException(String module, String code, Object[] args, String defaultMessage)
  29. {
  30. this.module = module;
  31. this.code = code;
  32. this.args = args;
  33. this.defaultMessage = defaultMessage;
  34. }
  35. public BaseException(String module, String code, Object[] args)
  36. {
  37. this(module, code, args, null);
  38. }
  39. public BaseException(String module, String defaultMessage)
  40. {
  41. this(module, null, null, defaultMessage);
  42. }
  43. public BaseException(String code, Object[] args)
  44. {
  45. this(null, code, args, null);
  46. }
  47. public BaseException(String defaultMessage)
  48. {
  49. this(null, null, null, defaultMessage);
  50. }
  51. @Override
  52. public String getMessage()
  53. {
  54. String message = null;
  55. if (!StringUtils.isEmpty(code))
  56. {
  57. message = MessageUtils.message(code, args);
  58. }
  59. if (message == null)
  60. {
  61. message = defaultMessage;
  62. }
  63. return message;
  64. }
  65. public String getModule()
  66. {
  67. return module;
  68. }
  69. public String getCode()
  70. {
  71. return code;
  72. }
  73. public Object[] getArgs()
  74. {
  75. return args;
  76. }
  77. public String getDefaultMessage()
  78. {
  79. return defaultMessage;
  80. }
  81. }