LANG-1364: ExceptionUtils#getRootCause(Throwable t) should return t if no lower level cause exists

This makes the behavior of getRootCause consistent with getRootCauseMessage and getRootCauseStackTrace.
This commit is contained in:
pascalschumacher 2018-02-11 14:19:56 +01:00
parent 3a4ac35798
commit 60412131f3
3 changed files with 5 additions and 4 deletions

View File

@ -46,6 +46,7 @@ The <action> type attribute can be add,update,fix,remove.
<body> <body>
<release version="3.8" date="2017-MM-DD" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10."> <release version="3.8" date="2017-MM-DD" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
<action issue="LANG-1364" type="fix" dev="pschumacher" due-to="Zheng Xie">ExceptionUtils#getRootCause(Throwable t) should return t if no lower level cause exists</action>
<action issue="LANG-1060" type="fix" dev="pschumacher" due-to="Piotr Kosmala">NumberUtils.isNumber assumes number starting with Zero</action> <action issue="LANG-1060" type="fix" dev="pschumacher" due-to="Piotr Kosmala">NumberUtils.isNumber assumes number starting with Zero</action>
<action issue="LANG-1375" type="fix" dev="kinow" due-to="Jerry Zhao">defaultString(final String str) in StringUtils to reuse defaultString(final String str, final String defaultStr)</action> <action issue="LANG-1375" type="fix" dev="kinow" due-to="Jerry Zhao">defaultString(final String str) in StringUtils to reuse defaultString(final String str, final String defaultStr)</action>
<action issue="LANG-1374" type="fix" dev="kinow" due-to="Jaswanth Bala">Parsing Json Array failed</action> <action issue="LANG-1374" type="fix" dev="kinow" due-to="Jaswanth Bala">Parsing Json Array failed</action>

View File

@ -179,11 +179,11 @@ public static Throwable getCause(final Throwable throwable, String[] methodNames
* *
* @param throwable the throwable to get the root cause for, may be null * @param throwable the throwable to get the root cause for, may be null
* @return the root cause of the <code>Throwable</code>, * @return the root cause of the <code>Throwable</code>,
* <code>null</code> if none found or null throwable input * <code>null</code> if null throwable input
*/ */
public static Throwable getRootCause(final Throwable throwable) { public static Throwable getRootCause(final Throwable throwable) {
final List<Throwable> list = getThrowableList(throwable); final List<Throwable> list = getThrowableList(throwable);
return list.size() < 2 ? null : list.get(list.size() - 1); return list.isEmpty() ? null : list.get(list.size() - 1);
} }
/** /**

View File

@ -147,10 +147,10 @@ public void testGetCause_ThrowableArray() {
@Test @Test
public void testGetRootCause_Throwable() { public void testGetRootCause_Throwable() {
assertSame(null, ExceptionUtils.getRootCause(null)); assertSame(null, ExceptionUtils.getRootCause(null));
assertSame(null, ExceptionUtils.getRootCause(withoutCause)); assertSame(withoutCause, ExceptionUtils.getRootCause(withoutCause));
assertSame(withoutCause, ExceptionUtils.getRootCause(nested)); assertSame(withoutCause, ExceptionUtils.getRootCause(nested));
assertSame(withoutCause, ExceptionUtils.getRootCause(withCause)); assertSame(withoutCause, ExceptionUtils.getRootCause(withCause));
assertSame(null, ExceptionUtils.getRootCause(jdkNoCause)); assertSame(jdkNoCause, ExceptionUtils.getRootCause(jdkNoCause));
assertSame(cyclicCause.getCause().getCause(), ExceptionUtils.getRootCause(cyclicCause)); assertSame(cyclicCause.getCause().getCause(), ExceptionUtils.getRootCause(cyclicCause));
} }