Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x: Error when tracking exception with unknown cause #4740

Merged
merged 1 commit into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/rx/exceptions/AssemblyStackTraceException.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ public void attachTo(Throwable exception) {

for (;;) {
if (exception.getCause() == null) {
exception.initCause(this);
try {
exception.initCause(this);
} catch (IllegalStateException e) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably rename e to ignored

RxJavaHooks.onError(new RuntimeException(
"Received an exception with a cause set to null, instead of being unset."
+ " To fix this, look down the chain of causes. The last exception had"
+ " a cause explicitly set to null. It should be unset instead.",
exception));
}
return;
}

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/rx/plugins/RxJavaHooksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;

import org.mockito.Mockito;
import rx.*;
import rx.Observable;
import rx.Scheduler.Worker;
Expand All @@ -39,6 +41,12 @@

public class RxJavaHooksTest {

public static class TestExceptionWithUnknownCause extends RuntimeException {
TestExceptionWithUnknownCause() {
super((Throwable) null);
}
}

static Observable<Integer> createObservable() {
return Observable.range(1, 5).map(new Func1<Integer, Integer>() {
@Override
Expand All @@ -48,6 +56,15 @@ public Integer call(Integer t) {
});
}

static Observable<Integer> createObservableThrowingUnknownCause() {
return Observable.range(1, 5).map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer t) {
throw new TestExceptionWithUnknownCause();
}
});
}

@Test
public void constructorShouldBePrivate() {
TestUtil.checkUtilityClass(RxJavaHooks.class);
Expand Down Expand Up @@ -89,6 +106,31 @@ public void assemblyTrackingObservable() {
}
}

@Test
public void assemblyTrackingObservableUnknownCause() {
RxJavaHooks.enableAssemblyTracking();
try {
final AtomicReference<Throwable> onErrorThrowableRef = new AtomicReference<Throwable>();
RxJavaHooks.setOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
onErrorThrowableRef.set(throwable);
}
});
TestSubscriber<Integer> ts = TestSubscriber.create();

createObservableThrowingUnknownCause().subscribe(ts);

ts.assertError(TestExceptionWithUnknownCause.class);

Throwable receivedError = onErrorThrowableRef.get();
assertNotNull(receivedError);
assertTrue(receivedError.getMessage().contains("cause set to null"));
} finally {
RxJavaHooks.reset();
}
}

static Single<Integer> createSingle() {
return Single.just(1).map(new Func1<Integer, Integer>() {
@Override
Expand Down