SMOException.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package com.java110.common.exception;
  2. import com.alibaba.fastjson.JSONObject;
  3. import java.io.PrintStream;
  4. import java.io.PrintWriter;
  5. /**
  6. * 无权限异常
  7. * Created by wuxw on 2018/4/14.
  8. */
  9. public class SMOException extends RuntimeException {
  10. private Result result;
  11. private Throwable cause = this;
  12. public SMOException(){}
  13. /**
  14. * 构造方法
  15. * @param result 返回值
  16. * @param cause 异常堆栈
  17. */
  18. public SMOException(Result result, Throwable cause) {
  19. super(result.getMsg(), cause);
  20. this.result = result;
  21. }
  22. /**
  23. * 构造方法
  24. * @param code 返回码
  25. * @param msg 错误消息
  26. */
  27. public SMOException(int code, String msg) {
  28. super(msg);
  29. this.result = new Result(code, msg);
  30. }
  31. public SMOException(String code, String msg) {
  32. super(msg);
  33. this.result = new Result(code, msg);
  34. }
  35. /**
  36. * 构造方法
  37. * @param result 返回值
  38. * @param detail 具体的返回消息
  39. */
  40. public SMOException(Result result, String detail) {
  41. super(result.getMsg() + "," + detail);
  42. this.result = new Result(result.getCode(), result.getMsg() + "," + detail);
  43. }
  44. /**
  45. * 构造方法
  46. * @param result 返回值
  47. * @param detail 具体的返回消息
  48. * @param cause 异常堆栈
  49. */
  50. public SMOException(Result result, String detail, Throwable cause) {
  51. super(result.getMsg() + "," + detail, cause);
  52. this.result = new Result(result.getCode(), result.getMsg() + "," + detail);
  53. }
  54. /**
  55. * 构造方法
  56. * @param code 返回码
  57. * @param msg 返回消息
  58. * @param cause 异常堆栈
  59. */
  60. public SMOException(int code, String msg, Throwable cause) {
  61. super(msg, cause);
  62. if(cause != null) {
  63. if(cause.getCause() != null) {
  64. msg += " cause:" + ExceptionUtils.populateExecption(cause.getCause(), 500);
  65. }
  66. msg += " StackTrace:"+ExceptionUtils.populateExecption(cause, 500);
  67. }
  68. this.result = new Result(code, msg);
  69. }
  70. /**
  71. * 构造方法
  72. * @param code 返回码
  73. * @param cause 异常堆栈
  74. */
  75. public SMOException(int code, Throwable cause) {
  76. super(cause);
  77. String msg = "";
  78. if(cause != null) {
  79. if(cause.getCause() != null) {
  80. msg += " cause:" + ExceptionUtils.populateExecption(cause.getCause(), 500);
  81. }
  82. msg += " StackTrace:"+ExceptionUtils.populateExecption(cause, 500);
  83. }
  84. this.result = new Result(code, msg);
  85. }
  86. /**
  87. *
  88. * TODO 简单描述该方法的实现功能(可选).
  89. * @see Throwable#getCause()
  90. */
  91. public synchronized Throwable getCause() {
  92. return (cause==this ? super.getCause() : cause);
  93. }
  94. /**
  95. * 返回异常消息
  96. * @return 异常消息
  97. */
  98. @Override
  99. public String getMessage() {
  100. return ExceptionUtils.buildMessage(super.getMessage(), getCause());
  101. }
  102. /**
  103. * 异常
  104. * @return
  105. */
  106. public String toJsonString() {
  107. JSONObject exceptionJson = JSONObject.parseObject("{\"exception\":{}");
  108. JSONObject exceptionJsonObj = exceptionJson.getJSONObject("exception");
  109. if (getResult() != null)
  110. exceptionJsonObj.putAll(JSONObject.parseObject(result.toString()));
  111. exceptionJsonObj.put("exceptionTrace",getMessage());
  112. return exceptionJsonObj.toString();
  113. }
  114. @Override
  115. public void printStackTrace(PrintStream ps) {
  116. ps.print("<exception>");
  117. if (getResult() != null) {
  118. ps.print(result.toString());
  119. }
  120. ps.append("<exceptionTrace>");
  121. Throwable cause = getCause();
  122. if (cause == null) {
  123. super.printStackTrace(ps);
  124. } else {
  125. ps.println(this);
  126. ps.print("Caused by: ");
  127. cause.printStackTrace(ps);
  128. }
  129. ps.append("</exceptionTrace>");
  130. ps.println("</exception>");
  131. }
  132. @Override
  133. public void printStackTrace(PrintWriter pw) {
  134. pw.print("<exception>");
  135. if (getResult() != null) {
  136. pw.print(result.toString());
  137. }
  138. pw.append("<exceptionTrace>");
  139. Throwable cause = getCause();
  140. if (cause == null) {
  141. super.printStackTrace(pw);
  142. } else {
  143. pw.println(this);
  144. pw.print("Caused by: ");
  145. cause.printStackTrace(pw);
  146. }
  147. pw.append("</exceptionTrace>");
  148. pw.println("</exception>");
  149. }
  150. /**
  151. * 返回异常值
  152. * @return 异常值对象
  153. */
  154. public Result getResult() {
  155. return result;
  156. }
  157. public void setResult(Result result) {
  158. this.result = result;
  159. }
  160. }