Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle illegal errors thrown from plugin #898

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions rxjava-core/src/main/java/rx/observers/SafeSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public void onNext(T args) {
protected void _onError(Throwable e) {
try {
RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
} catch (Throwable pluginException) {
handlePluginException(pluginException);
}
try {
actual.onError(e);
} catch (Throwable e2) {
if (e2 instanceof OnErrorNotImplementedException) {
Expand All @@ -134,7 +138,11 @@ protected void _onError(Throwable e) {
try {
unsubscribe();
} catch (Throwable unsubscribeException) {
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
try {
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
} catch (Throwable pluginException) {
handlePluginException(pluginException);
}
throw new RuntimeException("Observer.onError not implemented and error while unsubscribing.", new CompositeException(Arrays.asList(e, unsubscribeException)));
}
throw (OnErrorNotImplementedException) e2;
Expand All @@ -144,11 +152,19 @@ protected void _onError(Throwable e) {
*
* https://github.com/Netflix/RxJava/issues/198
*/
RxJavaPlugins.getInstance().getErrorHandler().handleError(e2);
try {
RxJavaPlugins.getInstance().getErrorHandler().handleError(e2);
} catch (Throwable pluginException) {
handlePluginException(pluginException);
}
try {
unsubscribe();
} catch (Throwable unsubscribeException) {
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
try {
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
} catch (Throwable pluginException) {
handlePluginException(pluginException);
}
throw new RuntimeException("Error occurred when trying to propagate error to Observer.onError and during unsubscription.", new CompositeException(Arrays.asList(e, e2, unsubscribeException)));
}

Expand All @@ -159,11 +175,25 @@ protected void _onError(Throwable e) {
try {
unsubscribe();
} catch (RuntimeException unsubscribeException) {
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
try {
RxJavaPlugins.getInstance().getErrorHandler().handleError(unsubscribeException);
} catch (Throwable pluginException) {
handlePluginException(pluginException);
}
throw unsubscribeException;
}
}

private void handlePluginException(Throwable pluginException) {
/*
* We don't want errors from the plugin to affect normal flow.
* Since the plugin should never throw this is a safety net
* and will complain loudly to System.err so it gets fixed.
*/
System.err.println("RxJavaErrorHandler threw an Exception. It shouldn't. => " + pluginException.getMessage());
pluginException.printStackTrace();
}

public Subscriber<? super T> getActual() {
return actual;
}
Expand Down