68 lines
1.7 KiB
Java
Raw Normal View History

2019-03-13 19:45:12 +08:00
package com.ruoyi.common.utils;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.commons.lang3.exception.ExceptionUtils;
/**
* 错误信息处理类
*
* @author ruoyi
*/
public class ExceptionUtil
{
/**
* 获取exception的详细错误信息
*/
public static String getExceptionMessage(Throwable e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
2022-02-12 21:23:07 +08:00
return sw.toString();
2019-03-13 19:45:12 +08:00
}
2021-05-24 10:37:39 +08:00
public static String getRootErrorMessage(Exception e)
2019-03-13 19:45:12 +08:00
{
Throwable root = ExceptionUtils.getRootCause(e);
root = (root == null ? e : root);
if (root == null)
{
return "";
}
String msg = root.getMessage();
if (msg == null)
{
return "null";
}
return StringUtils.defaultString(msg);
}
/**
* 检测异常e被触发的原因是不是因为异常cause
*
* @param e 捕获的异常
* @param cause 异常触发原因
* @return 如果异常e是由cause类异常触发则返回true否则返回false
*/
public static boolean isCausedBy(final Throwable e, final Class<? extends Throwable> cause)
{
if (cause.isAssignableFrom(e.getClass()))
{
return true;
}
else
{
Throwable t = e.getCause();
while (t != null && t != e)
{
if (cause.isAssignableFrom(t.getClass()))
{
return true;
}
t = t.getCause();
}
return false;
}
}
2019-03-13 19:45:12 +08:00
}