ExceptionUtil.java 963 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.ruoyi.common.utils;
  2. import java.io.PrintWriter;
  3. import java.io.StringWriter;
  4. import org.apache.commons.lang3.exception.ExceptionUtils;
  5. /**
  6. * 错误信息处理类。
  7. *
  8. * @author ruoyi
  9. */
  10. public class ExceptionUtil
  11. {
  12. /**
  13. * 获取exception的详细错误信息。
  14. */
  15. public static String getExceptionMessage(Throwable e)
  16. {
  17. StringWriter sw = new StringWriter();
  18. e.printStackTrace(new PrintWriter(sw, true));
  19. String str = sw.toString();
  20. return str;
  21. }
  22. public static String getRootErrorMessage(Exception e)
  23. {
  24. Throwable root = ExceptionUtils.getRootCause(e);
  25. root = (root == null ? e : root);
  26. if (root == null)
  27. {
  28. return "";
  29. }
  30. String msg = root.getMessage();
  31. if (msg == null)
  32. {
  33. return "null";
  34. }
  35. return StringUtils.defaultString(msg);
  36. }
  37. }