From 2e100d2797af950a09693ec29cb28bcf47435644 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Sun, 25 Sep 2016 22:13:05 +0200 Subject: [PATCH] 2.x: RxJavaPlugins - Don't pass null throwable down to Error Handler (#4603) --- .../io/reactivex/plugins/RxJavaPlugins.java | 13 +++++----- .../reactivex/plugins/RxJavaPluginsTest.java | 24 ++++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/reactivex/plugins/RxJavaPlugins.java b/src/main/java/io/reactivex/plugins/RxJavaPlugins.java index 8a956bd8aa..e9114317d4 100644 --- a/src/main/java/io/reactivex/plugins/RxJavaPlugins.java +++ b/src/main/java/io/reactivex/plugins/RxJavaPlugins.java @@ -252,23 +252,22 @@ public static Scheduler onComputationScheduler(Scheduler defaultScheduler) { */ public static void onError(Throwable error) { Consumer 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); } diff --git a/src/test/java/io/reactivex/plugins/RxJavaPluginsTest.java b/src/test/java/io/reactivex/plugins/RxJavaPluginsTest.java index ab37f608e9..e14bf2c47f 100644 --- a/src/test/java/io/reactivex/plugins/RxJavaPluginsTest.java +++ b/src/test/java/io/reactivex/plugins/RxJavaPluginsTest.java @@ -16,6 +16,7 @@ package io.reactivex.plugins; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.Assert.*; import java.io.IOException; @@ -1695,4 +1696,25 @@ public void onComplete() { .assertComplete(); } -} \ No newline at end of file + @Test + public void onErrorNull() { + try { + final AtomicReference t = new AtomicReference(); + + RxJavaPlugins.setErrorHandler(new Consumer() { + @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(); + } + } +}