Skip to content

Commit aa1c4ed

Browse files
authored
1.x: replace non-serializable value of OnNextValue with its toString (#4841)
1 parent 2cdf1c0 commit aa1c4ed

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

src/main/java/rx/exceptions/OnErrorThrowable.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616
package rx.exceptions;
1717

18-
import java.util.HashSet;
19-
import java.util.Set;
18+
import java.io.*;
19+
import java.util.*;
2020

21-
import rx.plugins.RxJavaErrorHandler;
22-
import rx.plugins.RxJavaPlugins;
21+
import rx.plugins.*;
2322

2423
/**
2524
* Represents a {@code Throwable} that an {@code Observable} might notify its subscribers of, but that then can
@@ -43,7 +42,17 @@ private OnErrorThrowable(Throwable exception) {
4342
private OnErrorThrowable(Throwable exception, Object value) {
4443
super(exception);
4544
hasValue = true;
46-
this.value = value;
45+
Object v;
46+
if (value instanceof Serializable) {
47+
v = value;
48+
} else {
49+
try {
50+
v = String.valueOf(value);
51+
} catch (Throwable ex) {
52+
v = ex.getMessage();
53+
}
54+
}
55+
this.value = v;
4756
}
4857

4958
/**
@@ -150,7 +159,17 @@ private static Set<Class<?>> create() {
150159
*/
151160
public OnNextValue(Object value) {
152161
super("OnError while emitting onNext value: " + renderValue(value));
153-
this.value = value;
162+
Object v;
163+
if (value instanceof Serializable) {
164+
v = value;
165+
} else {
166+
try {
167+
v = String.valueOf(value);
168+
} catch (Throwable ex) {
169+
v = ex.getMessage();
170+
}
171+
}
172+
this.value = v;
154173
}
155174

156175
/**
@@ -177,7 +196,7 @@ public Object getValue() {
177196
* @return a string version of the object if primitive or managed through error plugin,
178197
* otherwise the class name of the object
179198
*/
180-
static String renderValue(Object value){
199+
static String renderValue(Object value) {
181200
if (value == null) {
182201
return "null";
183202
}

src/test/java/rx/exceptions/OnNextValueTest.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@
1515
*/
1616
package rx.exceptions;
1717

18+
import static org.junit.Assert.*;
19+
20+
import java.io.*;
21+
1822
import org.junit.Test;
1923

20-
import rx.Observable;
21-
import rx.Observer;
24+
import rx.*;
2225
import rx.exceptions.OnErrorThrowable.OnNextValue;
2326
import rx.functions.Func1;
2427

25-
import java.io.PrintWriter;
26-
import java.io.StringWriter;
27-
28-
import static org.junit.Assert.assertEquals;
29-
import static org.junit.Assert.assertTrue;
30-
import static org.junit.Assert.fail;
31-
3228
/**
3329
* ```java
3430
* public OnNextValue(Object value) {
@@ -166,4 +162,32 @@ public void testRenderDouble() {
166162
public void testRenderVoid() {
167163
assertEquals("null", OnNextValue.renderValue((Void) null));
168164
}
165+
166+
static class Value {
167+
@Override
168+
public String toString() {
169+
return "Value";
170+
}
171+
}
172+
173+
@Test
174+
public void nonSerializableValue() throws Exception {
175+
Throwable e = OnErrorThrowable.addValueAsLastCause(new RuntimeException(), new Value());
176+
177+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
178+
ObjectOutputStream oos = new ObjectOutputStream(bout);
179+
oos.writeObject(e);
180+
oos.close();
181+
182+
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
183+
ObjectInputStream ois = new ObjectInputStream(bin);
184+
185+
Throwable f = (Throwable)ois.readObject();
186+
187+
ois.close();
188+
189+
Object v = ((OnNextValue)f.getCause()).getValue();
190+
191+
assertEquals("Value", v);
192+
}
169193
}

0 commit comments

Comments
 (0)