Skip to content

Commit 471d590

Browse files
jschneiderakarnokd
authored andcommitted
use bounded wildcards for errorHandler (fixes #5045) (#5049)
* use bounded wildcards for errorHandler (fixes #5045) * add test to ensure signature
1 parent b5c0fb8 commit 471d590

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
*/
5050
public final class RxJavaPlugins {
5151
@Nullable
52-
static volatile Consumer<Throwable> errorHandler;
52+
static volatile Consumer<? super Throwable> errorHandler;
5353

5454
@Nullable
5555
static volatile Function<Runnable, Runnable> onScheduleHandler;
@@ -197,7 +197,7 @@ public static Function<Scheduler, Scheduler> getComputationSchedulerHandler() {
197197
* @return the hook consumer, may be null
198198
*/
199199
@Nullable
200-
public static Consumer<Throwable> getErrorHandler() {
200+
public static Consumer<? super Throwable> getErrorHandler() {
201201
return errorHandler;
202202
}
203203

@@ -356,7 +356,7 @@ public static Scheduler onComputationScheduler(@NonNull Scheduler defaultSchedul
356356
* @param error the error to report
357357
*/
358358
public static void onError(@NonNull Throwable error) {
359-
Consumer<Throwable> f = errorHandler;
359+
Consumer<? super Throwable> f = errorHandler;
360360

361361
if (error == null) {
362362
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
@@ -497,7 +497,7 @@ public static void setComputationSchedulerHandler(@Nullable Function<Scheduler,
497497
* Sets the specific hook function.
498498
* @param handler the hook function to set, null allowed
499499
*/
500-
public static void setErrorHandler(@Nullable Consumer<Throwable> handler) {
500+
public static void setErrorHandler(@Nullable Consumer<? super Throwable> handler) {
501501
if (lockdown) {
502502
throw new IllegalStateException("Plugins can't be changed anymore");
503503
}

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,28 @@ public void uncaughtException(Thread t, Throwable e) {
11681168
}
11691169
}
11701170

1171-
@SuppressWarnings({ "rawtypes", "unchecked" })
1171+
/**
1172+
* Ensure setErrorHandler() accepts a consumer with "? super Throwable"
1173+
*/
1174+
@Test
1175+
public void onErrorWithSuper() throws Exception {
1176+
try {
1177+
Consumer<? super Throwable> errorHandler = new Consumer<Throwable>() {
1178+
@Override
1179+
public void accept(Throwable t) {
1180+
throw new TestException("Forced failure 2");
1181+
}
1182+
};
1183+
RxJavaPlugins.setErrorHandler(errorHandler);
1184+
1185+
Consumer<? super Throwable> errorHandler1 = RxJavaPlugins.getErrorHandler();
1186+
assertSame(errorHandler, errorHandler1);
1187+
} finally {
1188+
RxJavaPlugins.reset();
1189+
}
1190+
}
1191+
1192+
@SuppressWarnings({"rawtypes", "unchecked" })
11721193
@Test
11731194
public void clearIsPassthrough() {
11741195
try {

0 commit comments

Comments
 (0)