Skip to content

Commit b50abaa

Browse files
author
shendley
committed
Added a renderValue function to create a string from unknown client objects without calling toString (PR #1401)
1 parent d23ff71 commit b50abaa

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

rxjava-core/src/main/java/rx/exceptions/OnErrorThrowable.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static class OnNextValue extends RuntimeException {
116116
* the item that the Observable was trying to emit at the time of the exception
117117
*/
118118
public OnNextValue(Object value) {
119-
super("OnError while emitting onNext.");
119+
super("OnError while emitting onNext value: " + renderValue(value));
120120
this.value = value;
121121
}
122122

@@ -129,5 +129,25 @@ public Object getValue() {
129129
return value;
130130
}
131131

132+
/**
133+
* Render the object if it is a basic type. This avoids the library making potentially expensive
134+
* or calls to toString() which may throw exceptions. See PR #1401 for details.
135+
*
136+
* @param value
137+
* the item that the Observable was trying to emit at the time of the exception
138+
* @return a string version of the object if primitive, otherwise the classname of the object
139+
*/
140+
private static String renderValue(Object value){
141+
if(value == null){
142+
return "null";
143+
}
144+
if(value.getClass().isPrimitive()){
145+
return value.toString();
146+
}
147+
if(value instanceof String){
148+
return (String)value;
149+
}
150+
return value.getClass().getSimpleName() + ".class";
151+
}
132152
}
133153
}

0 commit comments

Comments
 (0)