Skip to content

Commit

Permalink
Merge pull request #3048 from akarnokd/CompositeExceptionNull
Browse files Browse the repository at this point in the history
CompositeException extra NPE protection
  • Loading branch information
benjchristensen committed Jul 14, 2015
2 parents 9269692 + 8951a33 commit df5de06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/rx/exceptions/CompositeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ public final class CompositeException extends RuntimeException {
public CompositeException(String messagePrefix, Collection<? extends Throwable> errors) {
Set<Throwable> deDupedExceptions = new LinkedHashSet<Throwable>();
List<Throwable> _exceptions = new ArrayList<Throwable>();
for (Throwable ex : errors) {
if (ex instanceof CompositeException) {
deDupedExceptions.addAll(((CompositeException) ex).getExceptions());
} else {
deDupedExceptions.add(ex);
if (errors != null) {
for (Throwable ex : errors) {
if (ex instanceof CompositeException) {
deDupedExceptions.addAll(((CompositeException) ex).getExceptions());
} else
if (ex != null) {
deDupedExceptions.add(ex);
} else {
deDupedExceptions.add(new NullPointerException());
}
}
} else {
deDupedExceptions.add(new NullPointerException());
}

_exceptions.addAll(deDupedExceptions);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/rx/exceptions/CompositeExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,17 @@ private static Throwable getRootCause(Throwable ex) {
}
}
}

@Test
public void testNullCollection() {
CompositeException composite = new CompositeException(null);
composite.getCause();
composite.printStackTrace();
}
@Test
public void testNullElement() {
CompositeException composite = new CompositeException(Arrays.asList((Throwable)null));
composite.getCause();
composite.printStackTrace();
}
}

0 comments on commit df5de06

Please sign in to comment.