Skip to content

Commit 84084da

Browse files
committed
Merge pull request #2992 from davidmoten/error-cause-primitives
Fix value rendering in error last cause for primitive types
2 parents 915e2ac + bd99265 commit 84084da

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

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

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

18+
import java.util.HashSet;
19+
import java.util.Set;
20+
1821
import rx.plugins.RxJavaErrorHandler;
1922
import rx.plugins.RxJavaPlugins;
2023

@@ -109,6 +112,27 @@ public static Throwable addValueAsLastCause(Throwable e, Object value) {
109112
public static class OnNextValue extends RuntimeException {
110113

111114
private static final long serialVersionUID = -3454462756050397899L;
115+
116+
// Lazy loaded singleton
117+
private static final class Primitives {
118+
119+
static final Set<Class<?>> INSTANCE = create();
120+
121+
private static Set<Class<?>> create() {
122+
Set<Class<?>> set = new HashSet<Class<?>>();
123+
set.add(Boolean.class);
124+
set.add(Character.class);
125+
set.add(Byte.class);
126+
set.add(Short.class);
127+
set.add(Integer.class);
128+
set.add(Long.class);
129+
set.add(Float.class);
130+
set.add(Double.class);
131+
// Void is another primitive but cannot be instantiated
132+
// and is caught by the null check in renderValue
133+
return set;
134+
}
135+
}
112136

113137
private final Object value;
114138

@@ -148,11 +172,11 @@ public Object getValue() {
148172
* @return a string version of the object if primitive or managed through error plugin,
149173
* otherwise the classname of the object
150174
*/
151-
private static String renderValue(Object value){
175+
static String renderValue(Object value){
152176
if (value == null) {
153177
return "null";
154178
}
155-
if (value.getClass().isPrimitive()) {
179+
if (Primitives.INSTANCE.contains(value.getClass())) {
156180
return value.toString();
157181
}
158182
if (value instanceof String) {

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
*/
1616
package rx.exceptions;
1717

18+
import org.junit.Assert;
1819
import org.junit.Test;
20+
1921
import rx.Observable;
2022
import rx.Observer;
23+
import rx.exceptions.OnErrorThrowable.OnNextValue;
2124
import rx.functions.Func1;
2225

2326
import java.io.PrintWriter;
2427
import java.io.StringWriter;
2528

29+
import static org.junit.Assert.assertEquals;
2630
import static org.junit.Assert.assertTrue;
2731
import static org.junit.Assert.fail;
2832

@@ -118,4 +122,49 @@ public BadToString call(BadToString badToString) {
118122
}).subscribe(observer);
119123

120124
}
125+
126+
@Test
127+
public void testRenderInteger() {
128+
assertEquals("123", OnNextValue.renderValue(123));
129+
}
130+
131+
@Test
132+
public void testRenderByte() {
133+
assertEquals("10", OnNextValue.renderValue((byte) 10));
134+
}
135+
136+
@Test
137+
public void testRenderBoolean() {
138+
assertEquals("true", OnNextValue.renderValue(true));
139+
}
140+
141+
@Test
142+
public void testRenderShort() {
143+
assertEquals("10", OnNextValue.renderValue((short) 10));
144+
}
145+
146+
@Test
147+
public void testRenderLong() {
148+
assertEquals("10", OnNextValue.renderValue(10L));
149+
}
150+
151+
@Test
152+
public void testRenderCharacter() {
153+
assertEquals("10", OnNextValue.renderValue(10L));
154+
}
155+
156+
@Test
157+
public void testRenderFloat() {
158+
assertEquals("10.0", OnNextValue.renderValue(10.0f));
159+
}
160+
161+
@Test
162+
public void testRenderDouble() {
163+
assertEquals("10.0", OnNextValue.renderValue(10.0));
164+
}
165+
166+
@Test
167+
public void testRenderVoid() {
168+
assertEquals("null", OnNextValue.renderValue((Void) null));
169+
}
121170
}

0 commit comments

Comments
 (0)