Skip to content

Commit 765d355

Browse files
Merge pull request #988 from benjchristensen/issue-969
OnErrorFailedException
2 parents fba0e6c + fb47ac8 commit 765d355

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public static RuntimeException propagate(Throwable t) {
4444
public static void throwIfFatal(Throwable t) {
4545
if (t instanceof OnErrorNotImplementedException) {
4646
throw (OnErrorNotImplementedException) t;
47+
} else if (t instanceof OnErrorFailedException) {
48+
Throwable cause = ((OnErrorFailedException) t).getCause();
49+
if (cause instanceof RuntimeException) {
50+
throw (RuntimeException) cause;
51+
} else {
52+
throw (OnErrorFailedException) t;
53+
}
4754
}
4855
// values here derived from https://github.com/Netflix/RxJava/issues/748#issuecomment-32471495
4956
else if (t instanceof StackOverflowError) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.exceptions;
17+
18+
import rx.Subscriber;
19+
20+
/**
21+
* Used for re-throwing errors thrown from {@link Subscriber#onError(Throwable)}.
22+
*
23+
* https://github.com/Netflix/RxJava/issues/969
24+
*/
25+
public class OnErrorFailedException extends RuntimeException {
26+
private static final long serialVersionUID = -419289748403337611L;
27+
28+
public OnErrorFailedException(String message, Throwable e) {
29+
super(message, e);
30+
}
31+
32+
public OnErrorFailedException(Throwable e) {
33+
super(e.getMessage(), e);
34+
}
35+
}

rxjava-core/src/main/java/rx/observers/SafeSubscriber.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import rx.Subscriber;
2222
import rx.exceptions.CompositeException;
2323
import rx.exceptions.Exceptions;
24+
import rx.exceptions.OnErrorFailedException;
2425
import rx.exceptions.OnErrorNotImplementedException;
2526
import rx.plugins.RxJavaPlugins;
2627

@@ -165,10 +166,10 @@ protected void _onError(Throwable e) {
165166
} catch (Throwable pluginException) {
166167
handlePluginException(pluginException);
167168
}
168-
throw new RuntimeException("Error occurred when trying to propagate error to Observer.onError and during unsubscription.", new CompositeException(Arrays.asList(e, e2, unsubscribeException)));
169+
throw new OnErrorFailedException("Error occurred when trying to propagate error to Observer.onError and during unsubscription.", new CompositeException(Arrays.asList(e, e2, unsubscribeException)));
169170
}
170171

171-
throw new RuntimeException("Error occurred when trying to propagate error to Observer.onError", new CompositeException(Arrays.asList(e, e2)));
172+
throw new OnErrorFailedException("Error occurred when trying to propagate error to Observer.onError", new CompositeException(Arrays.asList(e, e2)));
172173
}
173174
}
174175
// if we did not throw above we will unsubscribe here, if onError failed then unsubscribe happens in the catch
@@ -180,7 +181,7 @@ protected void _onError(Throwable e) {
180181
} catch (Throwable pluginException) {
181182
handlePluginException(pluginException);
182183
}
183-
throw unsubscribeException;
184+
throw new OnErrorFailedException(unsubscribeException);
184185
}
185186
}
186187

rxjava-core/src/test/java/rx/exceptions/ExceptionsTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package rx.exceptions;
1717

18+
import static org.junit.Assert.assertTrue;
19+
import static org.junit.Assert.fail;
20+
1821
import org.junit.Test;
1922

2023
import rx.Observable;
@@ -134,4 +137,34 @@ public void onNext(Integer t) {
134137
});
135138
}
136139

140+
/**
141+
* https://github.com/Netflix/RxJava/issues/969
142+
*/
143+
@Test
144+
public void testOnErrorExceptionIsThrown() {
145+
try {
146+
Observable.error(new IllegalArgumentException("original exception")).subscribe(new Observer<Object>() {
147+
@Override
148+
public void onCompleted() {
149+
150+
}
151+
152+
@Override
153+
public void onError(Throwable e) {
154+
throw new IllegalStateException("This should be thrown");
155+
}
156+
157+
@Override
158+
public void onNext(Object o) {
159+
160+
}
161+
});
162+
fail("expecting an exception to be thrown");
163+
} catch (CompositeException t) {
164+
CompositeException ce = (CompositeException) t;
165+
assertTrue(ce.getExceptions().get(0) instanceof IllegalArgumentException);
166+
assertTrue(ce.getExceptions().get(1) instanceof IllegalStateException);
167+
}
168+
}
169+
137170
}

rxjava-core/src/test/java/rx/observers/SafeObserverTest.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
*/
1616
package rx.observers;
1717

18-
import static org.junit.Assert.*;
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
1923

2024
import java.util.concurrent.atomic.AtomicReference;
2125

2226
import org.junit.Test;
2327

2428
import rx.Subscriber;
2529
import rx.exceptions.CompositeException;
30+
import rx.exceptions.OnErrorFailedException;
2631
import rx.exceptions.OnErrorNotImplementedException;
2732
import rx.functions.Action0;
2833
import rx.subscriptions.Subscriptions;
@@ -215,7 +220,8 @@ public void call() {
215220
assertEquals("failed", onError.get().getMessage());
216221

217222
// now assert the exception that was thrown
218-
assertTrue(e instanceof SafeObserverTestException);
223+
OnErrorFailedException onErrorFailedException = (OnErrorFailedException) e;
224+
assertTrue(onErrorFailedException.getCause() instanceof SafeObserverTestException);
219225
assertEquals("failure from unsubscribe", e.getMessage());
220226
}
221227
}

0 commit comments

Comments
 (0)