[New] Added ExceptionHelper.getRootCauseMessage(Throwable)

This commit is contained in:
Robert von Burg 2022-01-10 22:52:41 +01:00
parent 58fb8bb7fb
commit 7cb6b3acdc
1 changed files with 15 additions and 3 deletions

View File

@ -40,9 +40,9 @@ public class ExceptionHelper {
public static String getCallerMethod(int depth) { public static String getCallerMethod(int depth) {
// TODO change to StackWalker: // TODO change to StackWalker:
// StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); // StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
// walker.walk(frames -> frames.skip(1) // walker.walk(frames -> frames.skip(1)
// .map((StackWalker.StackFrame s) -> s.getDeclaringClass() + "." + s.getMethodName()).findFirst()); // .map((StackWalker.StackFrame s) -> s.getDeclaringClass() + "." + s.getMethodName()).findFirst());
StackTraceElement element = new Throwable().getStackTrace()[depth]; StackTraceElement element = new Throwable().getStackTrace()[depth];
return element.getClassName() + "." + element.getMethodName(); return element.getClassName() + "." + element.getMethodName();
@ -207,6 +207,18 @@ public class ExceptionHelper {
return t; return t;
} }
/**
* Returns {@link #getExceptionMessage(Throwable, boolean)} for the root cause of the given {@link Throwable}
*
* @param throwable
* the throwable for which to get the message of the root cause
*
* @return {@link #getExceptionMessage(Throwable, boolean)} for the root cause of the given {@link Throwable}
*/
public static String getRootCauseMessage(Throwable throwable) {
return getExceptionMessage(getRootCause(throwable), true);
}
/** /**
* Walks the causes for the given {@link Throwable} and sees if the given cause exists * Walks the causes for the given {@link Throwable} and sees if the given cause exists
* *