-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
2.x: wrap undeliverable errors #5080
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.exceptions; | ||
|
||
import io.reactivex.annotations.Experimental; | ||
|
||
/** | ||
* Explicitly named exception to indicate a Reactive-Streams | ||
* protocol violation. | ||
* @since 2.0.6 - experimental | ||
*/ | ||
@Experimental | ||
public final class ProtocolViolationException extends IllegalStateException { | ||
|
||
private static final long serialVersionUID = 1644750035281290266L; | ||
|
||
/** | ||
* Creates an instance with the given message. | ||
* @param message the message | ||
*/ | ||
public ProtocolViolationException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: 2017 |
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.exceptions; | ||
|
||
import io.reactivex.annotations.Experimental; | ||
|
||
/** | ||
* Wrapper for Throwable errors that are sent to `RxJavaPlugins.onError`. | ||
* @since 2.0.6 - experimental | ||
*/ | ||
@Experimental | ||
public final class UndeliverableException extends IllegalStateException { | ||
|
||
private static final long serialVersionUID = 1644750035281290266L; | ||
|
||
/** | ||
* Construct an instance by wrapping the given, non-null | ||
* cause Throwable. | ||
* @param cause the cause, not null | ||
*/ | ||
public UndeliverableException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.annotations.NonNull; | ||
import io.reactivex.annotations.Nullable; | ||
import io.reactivex.exceptions.*; | ||
import io.reactivex.flowables.ConnectableFlowable; | ||
import io.reactivex.functions.BiFunction; | ||
import io.reactivex.functions.BooleanSupplier; | ||
|
@@ -360,6 +361,10 @@ public static void onError(@NonNull Throwable error) { | |
|
||
if (error == null) { | ||
error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources."); | ||
} else { | ||
if (!isBug(error)) { | ||
error = new UndeliverableException(error); | ||
} | ||
} | ||
|
||
if (f != null) { | ||
|
@@ -377,6 +382,46 @@ public static void onError(@NonNull Throwable error) { | |
uncaught(error); | ||
} | ||
|
||
/** | ||
* Checks if the given error is one of the already named | ||
* bug cases that should pass through {@link #onError(Throwable)} | ||
* as is. | ||
* @param error the error to check | ||
* @return true if the error should pass throug, false if | ||
* it may be wrapped into an UndeliverableException | ||
*/ | ||
static boolean isBug(Throwable error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. A few tests have been reverted from assertUndeliverable to assertError. I've also added an isBug unit test. |
||
// user forgot to add the onError handler in subscribe | ||
if (error instanceof OnErrorNotImplementedException) { | ||
return true; | ||
} | ||
// the sender didn't honor the request amount | ||
// it's either due to an operator bug or concurrent onNext | ||
if (error instanceof MissingBackpressureException) { | ||
return true; | ||
} | ||
// general protocol violations | ||
// it's either due to an operator bug or concurrent onNext | ||
if (error instanceof IllegalStateException) { | ||
return true; | ||
} | ||
// nulls are generally not allowed | ||
// likely an operator bug or missing null-check | ||
if (error instanceof NullPointerException) { | ||
return true; | ||
} | ||
// bad arguments, likely invalid user input | ||
if (error instanceof IllegalArgumentException) { | ||
return true; | ||
} | ||
// Crash while handling an exception | ||
if (error instanceof CompositeException) { | ||
return true; | ||
} | ||
// everything else is probably due to lifecycle limits | ||
return false; | ||
} | ||
|
||
static void uncaught(@NonNull Throwable error) { | ||
Thread currentThread = Thread.currentThread(); | ||
UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: 2017
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All our headers are like this, no current year.