Common root cause introspection algorithm in NestedExceptionUtils
Issue: SPR-15510
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -80,13 +80,7 @@ public abstract class NestedCheckedException extends Exception {
|
||||
* @return the innermost exception, or {@code null} if none
|
||||
*/
|
||||
public Throwable getRootCause() {
|
||||
Throwable rootCause = null;
|
||||
Throwable cause = getCause();
|
||||
while (cause != null && cause != rootCause) {
|
||||
rootCause = cause;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return rootCause;
|
||||
return NestedExceptionUtils.getRootCause(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,17 +39,48 @@ public abstract class NestedExceptionUtils {
|
||||
* @return the full exception message
|
||||
*/
|
||||
public static String buildMessage(String message, Throwable cause) {
|
||||
if (cause != null) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (message != null) {
|
||||
sb.append(message).append("; ");
|
||||
}
|
||||
sb.append("nested exception is ").append(cause);
|
||||
return sb.toString();
|
||||
}
|
||||
else {
|
||||
if (cause == null) {
|
||||
return message;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(64);
|
||||
if (message != null) {
|
||||
sb.append(message).append("; ");
|
||||
}
|
||||
sb.append("nested exception is ").append(cause);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the innermost cause of the given exception, if any.
|
||||
* @param original the original exception to introspect
|
||||
* @return the innermost exception, or {@code null} if none
|
||||
* @since 4.3.9
|
||||
*/
|
||||
public static Throwable getRootCause(Throwable original) {
|
||||
if (original == null) {
|
||||
return null;
|
||||
}
|
||||
Throwable rootCause = null;
|
||||
Throwable cause = original.getCause();
|
||||
while (cause != null && cause != rootCause) {
|
||||
rootCause = cause;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return rootCause;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the most specific cause of the given exception, that is,
|
||||
* either the innermost cause (root cause) or the exception itself.
|
||||
* <p>Differs from {@link #getRootCause} in that it falls back
|
||||
* to the original exception if there is no root cause.
|
||||
* @param original the original exception to introspect
|
||||
* @return the most specific cause (never {@code null})
|
||||
* @since 4.3.9
|
||||
*/
|
||||
public static Throwable getMostSpecificCause(Throwable original) {
|
||||
Throwable rootCause = getRootCause(original);
|
||||
return (rootCause != null ? rootCause : original);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -81,13 +81,7 @@ public abstract class NestedRuntimeException extends RuntimeException {
|
||||
* @since 2.0
|
||||
*/
|
||||
public Throwable getRootCause() {
|
||||
Throwable rootCause = null;
|
||||
Throwable cause = getCause();
|
||||
while (cause != null && cause != rootCause) {
|
||||
rootCause = cause;
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return rootCause;
|
||||
return NestedExceptionUtils.getRootCause(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user