-
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
Implement hook to render specific types in OnNextValue #2632
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
|
||
import rx.Observable; | ||
import rx.Subscriber; | ||
import rx.annotations.Experimental; | ||
import rx.exceptions.Exceptions; | ||
import rx.exceptions.OnErrorThrowable; | ||
|
||
/** | ||
* Abstract class for defining error handling logic in addition to the normal | ||
|
@@ -25,6 +28,8 @@ | |
* For example, all {@code Exception}s can be logged using this handler even if | ||
* {@link Subscriber#onError(Throwable)} is ignored or not provided when an {@link Observable} is subscribed to. | ||
* <p> | ||
* This plugin is also responsible for augmenting rendering of {@link OnErrorThrowable.OnNextValue}. | ||
* <p> | ||
* See {@link RxJavaPlugins} or the RxJava GitHub Wiki for information on configuring plugins: <a | ||
* href="https://github.com/ReactiveX/RxJava/wiki/Plugins">https://github.com/ReactiveX/RxJava/wiki/Plugins</a>. | ||
*/ | ||
|
@@ -44,4 +49,52 @@ public void handleError(Throwable e) { | |
// do nothing by default | ||
} | ||
|
||
protected static final String ERROR_IN_RENDERING_SUFFIX = ".errorRendering"; | ||
|
||
/** | ||
* Receives items causing {@link OnErrorThrowable.OnNextValue} and gives a chance to choose the String | ||
* representation of the item in the OnNextValue stacktrace rendering. Returns null if this type of item | ||
* is not managed and should use default rendering. | ||
* <p> | ||
* Note that primitive types are always rendered as their toString() value. | ||
* <p> | ||
* If a {@code Throwable} is caught when rendering, this will fallback to the item's classname suffixed by | ||
* {@value #ERROR_IN_RENDERING_SUFFIX}. | ||
* | ||
* @param item the last emitted item, that caused the exception wrapped in {@link OnErrorThrowable.OnNextValue}. | ||
* @return a short {@link String} representation of the item if one is known for its type, or null for default. | ||
*/ | ||
@Experimental | ||
public final String handleOnNextValueRendering(Object item) { | ||
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. Can you add |
||
|
||
try { | ||
return render(item); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} catch (Throwable t) { | ||
Exceptions.throwIfFatal(t); | ||
} | ||
return item.getClass().getName() + ERROR_IN_RENDERING_SUFFIX; | ||
} | ||
|
||
/** | ||
* Override this method to provide rendering for specific types other than primitive types and null. | ||
* <p> | ||
* For performance and overhead reasons, this should should limit to a safe production of a short {@code String} | ||
* (as large renderings will bloat up the stacktrace). Prefer to try/catch({@code Throwable}) all code | ||
* inside this method implementation. | ||
* <p> | ||
* If a {@code Throwable} is caught when rendering, this will fallback to the item's classname suffixed by | ||
* {@value #ERROR_IN_RENDERING_SUFFIX}. | ||
* | ||
* @param item the last emitted item, that caused the exception wrapped in {@link OnErrorThrowable.OnNextValue}. | ||
* @return a short {@link String} representation of the item if one is known for its type, or null for default. | ||
* @throws InterruptedException if the rendering thread is interrupted | ||
*/ | ||
@Experimental | ||
protected String render (Object item) throws InterruptedException { | ||
//do nothing by default | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Perhaps the
ErrorHandler
should be statically referenced once at the top of this class?