Skip to content

Commit

Permalink
1.x: replace non-serializable value of OnNextValue with its toString (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd authored Nov 12, 2016
1 parent 2cdf1c0 commit aa1c4ed
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
33 changes: 26 additions & 7 deletions src/main/java/rx/exceptions/OnErrorThrowable.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
package rx.exceptions;

import java.util.HashSet;
import java.util.Set;
import java.io.*;
import java.util.*;

import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;
import rx.plugins.*;

/**
* Represents a {@code Throwable} that an {@code Observable} might notify its subscribers of, but that then can
Expand All @@ -43,7 +42,17 @@ private OnErrorThrowable(Throwable exception) {
private OnErrorThrowable(Throwable exception, Object value) {
super(exception);
hasValue = true;
this.value = value;
Object v;
if (value instanceof Serializable) {
v = value;
} else {
try {
v = String.valueOf(value);
} catch (Throwable ex) {
v = ex.getMessage();
}
}
this.value = v;
}

/**
Expand Down Expand Up @@ -150,7 +159,17 @@ private static Set<Class<?>> create() {
*/
public OnNextValue(Object value) {
super("OnError while emitting onNext value: " + renderValue(value));
this.value = value;
Object v;
if (value instanceof Serializable) {
v = value;
} else {
try {
v = String.valueOf(value);
} catch (Throwable ex) {
v = ex.getMessage();
}
}
this.value = v;
}

/**
Expand All @@ -177,7 +196,7 @@ public Object getValue() {
* @return a string version of the object if primitive or managed through error plugin,
* otherwise the class name of the object
*/
static String renderValue(Object value){
static String renderValue(Object value) {
if (value == null) {
return "null";
}
Expand Down
42 changes: 33 additions & 9 deletions src/test/java/rx/exceptions/OnNextValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
package rx.exceptions;

import static org.junit.Assert.*;

import java.io.*;

import org.junit.Test;

import rx.Observable;
import rx.Observer;
import rx.*;
import rx.exceptions.OnErrorThrowable.OnNextValue;
import rx.functions.Func1;

import java.io.PrintWriter;
import java.io.StringWriter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* ```java
* public OnNextValue(Object value) {
Expand Down Expand Up @@ -166,4 +162,32 @@ public void testRenderDouble() {
public void testRenderVoid() {
assertEquals("null", OnNextValue.renderValue((Void) null));
}

static class Value {
@Override
public String toString() {
return "Value";
}
}

@Test
public void nonSerializableValue() throws Exception {
Throwable e = OnErrorThrowable.addValueAsLastCause(new RuntimeException(), new Value());

ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bout);
oos.writeObject(e);
oos.close();

ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bin);

Throwable f = (Throwable)ois.readObject();

ois.close();

Object v = ((OnNextValue)f.getCause()).getValue();

assertEquals("Value", v);
}
}

0 comments on commit aa1c4ed

Please # to comment.