Skip to content
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

2.x: RxJavaPlugins - Don't pass null throwable down to Error Handler #4603

Merged
merged 1 commit into from
Sep 25, 2016
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
13 changes: 6 additions & 7 deletions src/main/java/io/reactivex/plugins/RxJavaPlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,22 @@ public static Scheduler onComputationScheduler(Scheduler defaultScheduler) {
*/
public static void onError(Throwable error) {
Consumer<Throwable> f = errorHandler;

if (error == null) {
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}

if (f != null) {
try {
f.accept(error);
return;
} catch (Throwable e) {
// Exceptions.throwIfFatal(e); TODO decide
if (error == null) {
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}
e.printStackTrace(); // NOPMD
uncaught(e);
}
} else {
if (error == null) {
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}
}

error.printStackTrace(); // NOPMD
uncaught(error);
}
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/io/reactivex/plugins/RxJavaPluginsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.reactivex.plugins;

import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.*;

import java.io.IOException;
Expand Down Expand Up @@ -1695,4 +1696,25 @@ public void onComplete() {
.assertComplete();
}

}
@Test
public void onErrorNull() {
try {
final AtomicReference<Throwable> t = new AtomicReference<Throwable>();

RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
@Override
public void accept(final Throwable throwable) throws Exception {
t.set(throwable);
}
});

RxJavaPlugins.onError(null);

final Throwable throwable = t.get();
assertEquals("onError called with null. Null values are generally not allowed in 2.x operators and sources.", throwable.getMessage());
assertTrue(throwable instanceof NullPointerException);
} finally {
RxJavaPlugins.reset();
}
}
}