Skip to content

Commit eb39120

Browse files
committed
Merge pull request #3620 from msavitskiy/1.x
1.x: Fix NPE in CompositeException when nested throws on initCause
2 parents 996da23 + 6e1b55f commit eb39120

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/main/java/rx/exceptions/CompositeException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ public synchronized Throwable getCause() {
118118
// we now have 'e' as the last in the chain
119119
try {
120120
chain.initCause(e);
121+
chain = chain.getCause();
121122
} catch (Throwable t) {
122123
// ignore
123124
// the javadocs say that some Throwables (depending on how they're made) will never
124125
// let me call initCause without blowing up even if it returns null
126+
chain = e;
125127
}
126-
chain = chain.getCause();
127128
}
128129
cause = _cause;
129130
}

src/test/java/rx/exceptions/CompositeExceptionTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,46 @@ public void testNullElement() {
178178
composite.getCause();
179179
composite.printStackTrace();
180180
}
181+
182+
@Test(timeout = 1000)
183+
public void testCompositeExceptionWithUnsupportedInitCause() {
184+
Throwable t = new Throwable() {
185+
@Override
186+
public synchronized Throwable initCause(Throwable cause) {
187+
throw new UnsupportedOperationException();
188+
}
189+
};
190+
CompositeException cex = new CompositeException(Arrays.asList(t, ex1));
191+
192+
System.err.println("----------------------------- print composite stacktrace");
193+
cex.printStackTrace();
194+
assertEquals(2, cex.getExceptions().size());
195+
196+
assertNoCircularReferences(cex);
197+
assertNotNull(getRootCause(cex));
198+
199+
System.err.println("----------------------------- print cause stacktrace");
200+
cex.getCause().printStackTrace();
201+
}
202+
203+
@Test(timeout = 1000)
204+
public void testCompositeExceptionWithNullInitCause() {
205+
Throwable t = new Throwable("ThrowableWithNullInitCause") {
206+
@Override
207+
public synchronized Throwable initCause(Throwable cause) {
208+
return null;
209+
}
210+
};
211+
CompositeException cex = new CompositeException(Arrays.asList(t, ex1));
212+
213+
System.err.println("----------------------------- print composite stacktrace");
214+
cex.printStackTrace();
215+
assertEquals(2, cex.getExceptions().size());
216+
217+
assertNoCircularReferences(cex);
218+
assertNotNull(getRootCause(cex));
219+
220+
System.err.println("----------------------------- print cause stacktrace");
221+
cex.getCause().printStackTrace();
222+
}
181223
}

0 commit comments

Comments
 (0)