MicroException.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
  3. */
  4. package com.crunii.micro.common.exception;
  5. import com.crunii.micro.common.dto.Result;
  6. import com.crunii.micro.common.utils.Slf4jUtil;
  7. import lombok.ToString;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.util.StringUtils;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * @author 田平 create 2014-12-8下午4:41:03
  14. */
  15. @ToString
  16. @Slf4j
  17. public class MicroException extends RuntimeException {
  18. private static final long serialVersionUID = 1L;
  19. public static final Map<String, String> messages = new HashMap<String, String>();
  20. public static final String LOCK_FAILURE = "1000";
  21. static {
  22. messages.put(LOCK_FAILURE, "lockId={} lock failure by reason:{}!");
  23. }
  24. private String code = Result.DEFAULT_ERROR_CODE;
  25. private String message;
  26. public MicroException() {
  27. }
  28. /**
  29. * <pre>
  30. * 注意不要使用code和message来调用这个方法
  31. * 需要直接设置这两个属性使用
  32. * MicroException me = new MicroException();
  33. * me.setCode(result.getCode());
  34. * me.setMessage(result.getMsg());
  35. * throw me;
  36. * </pre>
  37. *
  38. * @param code
  39. * @param args
  40. */
  41. public MicroException(String code, Object... args) {
  42. this(code, null, args);
  43. }
  44. public MicroException(String code, Throwable cause, Object... args) {
  45. super(cause);
  46. this.code = code;
  47. this.message = getMessageDetail(code, cause, args);
  48. }
  49. private static String getMessageDetail(String code, Throwable cause, Object... args) {
  50. String pattern = messages.get(code);
  51. if (!StringUtils.hasLength(pattern)) {
  52. throw new RuntimeException("错误编码code:" + code + " 未定义!");
  53. }
  54. if (args != null && args.length > 0) {
  55. Object[] params = new Object[args.length + 1];
  56. System.arraycopy(args, 0, params, 0, args.length);
  57. params[args.length] = cause;
  58. return Slf4jUtil.formatException(pattern, params);
  59. } else {
  60. return Slf4jUtil.formatException(pattern, cause);
  61. }
  62. }
  63. public MicroException(ErrorCode errorCode) {
  64. this.code = Integer.toString(errorCode.getCode());
  65. this.message = errorCode.getMsg();
  66. }
  67. public String getMessage() {
  68. return message;
  69. }
  70. public void setMessage(String message) {
  71. this.message = message;
  72. }
  73. public String getCode() {
  74. return code;
  75. }
  76. public void setCode(String code) {
  77. this.code = code;
  78. }
  79. public static String getErrorMsg(Throwable e) {
  80. return Slf4jUtil.getErrorMsg(e);
  81. }
  82. public static String getMaxDesc(Object desc) {
  83. return Slf4jUtil.getMaxDesc(desc);
  84. }
  85. public static void main(String[] args) {
  86. MicroException m = new MicroException("1015", null, "OSHIT", "1231");
  87. log.error("测试错误!", m);
  88. }
  89. }