Skip to content

CompositeException extra NPE protection #3048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move if (ex != null) { to the same line of else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This is my style of coding and there is no style guide for RxJava anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was unintentional. Never mind. See this style for the first time :)

deDupedExceptions.add(ex);
} else {
deDupedExceptions.add(new NullPointerException());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just throwing NullPointerException here? If that causes other issue, could you add some useful exception message, such as, Should not add null Throwable to CompositeException? So that people can quickly figure out it's an application bug instead of an RxJava bug.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like throwing exception from an exception? Since you only have the option to detect null here, throwing an NPE would have the same stacktrace as these new NPEs, but with the exception that they will further 'crash' the application. In my way, these inconsistencies get captured and propagated with the CompositeException itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

}
}
} 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();
}
}