File tree 2 files changed +59
-16
lines changed
2 files changed +59
-16
lines changed Original file line number Diff line number Diff line change 15
15
*/
16
16
package rx .exceptions ;
17
17
18
- import java .util . HashSet ;
19
- import java .util .Set ;
18
+ import java .io .* ;
19
+ import java .util .* ;
20
20
21
- import rx .plugins .RxJavaErrorHandler ;
22
- import rx .plugins .RxJavaPlugins ;
21
+ import rx .plugins .*;
23
22
24
23
/**
25
24
* 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) {
43
42
private OnErrorThrowable (Throwable exception , Object value ) {
44
43
super (exception );
45
44
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 ;
47
56
}
48
57
49
58
/**
@@ -150,7 +159,17 @@ private static Set<Class<?>> create() {
150
159
*/
151
160
public OnNextValue (Object value ) {
152
161
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 ;
154
173
}
155
174
156
175
/**
@@ -177,7 +196,7 @@ public Object getValue() {
177
196
* @return a string version of the object if primitive or managed through error plugin,
178
197
* otherwise the class name of the object
179
198
*/
180
- static String renderValue (Object value ){
199
+ static String renderValue (Object value ) {
181
200
if (value == null ) {
182
201
return "null" ;
183
202
}
Original file line number Diff line number Diff line change 15
15
*/
16
16
package rx .exceptions ;
17
17
18
+ import static org .junit .Assert .*;
19
+
20
+ import java .io .*;
21
+
18
22
import org .junit .Test ;
19
23
20
- import rx .Observable ;
21
- import rx .Observer ;
24
+ import rx .*;
22
25
import rx .exceptions .OnErrorThrowable .OnNextValue ;
23
26
import rx .functions .Func1 ;
24
27
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
-
32
28
/**
33
29
* ```java
34
30
* public OnNextValue(Object value) {
@@ -166,4 +162,32 @@ public void testRenderDouble() {
166
162
public void testRenderVoid () {
167
163
assertEquals ("null" , OnNextValue .renderValue ((Void ) null ));
168
164
}
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
+ }
169
193
}
You can’t perform that action at this time.
0 commit comments