Skip to content

Commit 2e100d2

Browse files
vanniktechakarnokd
authored andcommitted
2.x: RxJavaPlugins - Don't pass null throwable down to Error Handler (#4603)
1 parent 73dde07 commit 2e100d2

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/main/java/io/reactivex/plugins/RxJavaPlugins.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,22 @@ public static Scheduler onComputationScheduler(Scheduler defaultScheduler) {
252252
*/
253253
public static void onError(Throwable error) {
254254
Consumer<Throwable> f = errorHandler;
255+
256+
if (error == null) {
257+
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
258+
}
259+
255260
if (f != null) {
256261
try {
257262
f.accept(error);
258263
return;
259264
} catch (Throwable e) {
260265
// Exceptions.throwIfFatal(e); TODO decide
261-
if (error == null) {
262-
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
263-
}
264266
e.printStackTrace(); // NOPMD
265267
uncaught(e);
266268
}
267-
} else {
268-
if (error == null) {
269-
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
270-
}
271269
}
270+
272271
error.printStackTrace(); // NOPMD
273272
uncaught(error);
274273
}

src/test/java/io/reactivex/plugins/RxJavaPluginsTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.reactivex.plugins;
1818

19+
import java.util.concurrent.atomic.AtomicReference;
1920
import static org.junit.Assert.*;
2021

2122
import java.io.IOException;
@@ -1695,4 +1696,25 @@ public void onComplete() {
16951696
.assertComplete();
16961697
}
16971698

1698-
}
1699+
@Test
1700+
public void onErrorNull() {
1701+
try {
1702+
final AtomicReference<Throwable> t = new AtomicReference<Throwable>();
1703+
1704+
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
1705+
@Override
1706+
public void accept(final Throwable throwable) throws Exception {
1707+
t.set(throwable);
1708+
}
1709+
});
1710+
1711+
RxJavaPlugins.onError(null);
1712+
1713+
final Throwable throwable = t.get();
1714+
assertEquals("onError called with null. Null values are generally not allowed in 2.x operators and sources.", throwable.getMessage());
1715+
assertTrue(throwable instanceof NullPointerException);
1716+
} finally {
1717+
RxJavaPlugins.reset();
1718+
}
1719+
}
1720+
}

0 commit comments

Comments
 (0)