SMOException.java 4.9 KB

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