Skip to content

Commit

Permalink
2.x: RxJavaPlugins - Don't pass null throwable down to Error Handler (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vanniktech authored and akarnokd committed Sep 25, 2016
1 parent 73dde07 commit 2e100d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
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();
}
}
}

0 comments on commit 2e100d2

Please sign in to comment.