Skip to content

Commit 7035cb6

Browse files
Merge pull request #898 from benjchristensen/safe-subscriber-plugin-error-handling
Handle illegal errors thrown from plugin
2 parents e676ddd + 26f8e83 commit 7035cb6

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

rxjava-core/src/main/java/rx/observers/SafeSubscriber.java

+34-4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public void onNext(T args) {
117117
protected void _onError(Throwable e) {
118118
try {
119119
RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
120+
} catch (Throwable pluginException) {
121+
handlePluginException(pluginException);
122+
}
123+
try {
120124
actual.onError(e);
121125
} catch (Throwable e2) {
122126
if (e2 instanceof OnErrorNotImplementedException) {
@@ -134,7 +138,11 @@ protected void _onError(Throwable e) {
134138
try {
135139
unsubscribe();
136140
} catch (Throwable unsubscribeException) {
137-
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
141+
try {
142+
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
143+
} catch (Throwable pluginException) {
144+
handlePluginException(pluginException);
145+
}
138146
throw new RuntimeException("Observer.onError not implemented and error while unsubscribing.", new CompositeException(Arrays.asList(e, unsubscribeException)));
139147
}
140148
throw (OnErrorNotImplementedException) e2;
@@ -144,11 +152,19 @@ protected void _onError(Throwable e) {
144152
*
145153
* https://github.com/Netflix/RxJava/issues/198
146154
*/
147-
RxJavaPlugins.getInstance().getErrorHandler().handleError(e2);
155+
try {
156+
RxJavaPlugins.getInstance().getErrorHandler().handleError(e2);
157+
} catch (Throwable pluginException) {
158+
handlePluginException(pluginException);
159+
}
148160
try {
149161
unsubscribe();
150162
} catch (Throwable unsubscribeException) {
151-
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
163+
try {
164+
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
165+
} catch (Throwable pluginException) {
166+
handlePluginException(pluginException);
167+
}
152168
throw new RuntimeException("Error occurred when trying to propagate error to Observer.onError and during unsubscription.", new CompositeException(Arrays.asList(e, e2, unsubscribeException)));
153169
}
154170

@@ -159,11 +175,25 @@ protected void _onError(Throwable e) {
159175
try {
160176
unsubscribe();
161177
} catch (RuntimeException unsubscribeException) {
162-
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
178+
try {
179+
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
180+
} catch (Throwable pluginException) {
181+
handlePluginException(pluginException);
182+
}
163183
throw unsubscribeException;
164184
}
165185
}
166186

187+
private void handlePluginException(Throwable pluginException) {
188+
/*
189+
* We don't want errors from the plugin to affect normal flow.
190+
* Since the plugin should never throw this is a safety net
191+
* and will complain loudly to System.err so it gets fixed.
192+
*/
193+
System.err.println("RxJavaErrorHandler threw an Exception. It shouldn't. => " + pluginException.getMessage());
194+
pluginException.printStackTrace();
195+
}
196+
167197
public Subscriber<? super T> getActual() {
168198
return actual;
169199
}

0 commit comments

Comments
 (0)