Skip to content

1.x: Do not hide original exception with RxJavaHooks.enableAssemblyTracking() #4213

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

Closed
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
52 changes: 0 additions & 52 deletions src/main/java/rx/exceptions/AssemblyStackTraceException.java

This file was deleted.

47 changes: 30 additions & 17 deletions src/main/java/rx/internal/operators/OnSubscribeOnAssembly.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package rx.internal.operators;

import java.util.ArrayList;
import java.util.List;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.exceptions.AssemblyStackTraceException;

/**
* Captures the current stack when it is instantiated, makes
Expand All @@ -30,7 +31,7 @@ public final class OnSubscribeOnAssembly<T> implements OnSubscribe<T> {

final OnSubscribe<T> source;

Copy link
Member

Choose a reason for hiding this comment

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

This field had a purpose to show the stacktrace in debug mode by viewing the field as string!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Idk, it consumes memory and I've never used it during debugging. You really need it? I think you should be able to get ~normal output from Arrays.toString(assemblyStacktrace) in the debugger

final String stacktrace;
final StackTraceElement[] assemblyStacktrace;

/**
* If set to true, the creation of PublisherOnAssembly will capture the raw
Expand All @@ -40,15 +41,14 @@ public final class OnSubscribeOnAssembly<T> implements OnSubscribe<T> {

public OnSubscribeOnAssembly(OnSubscribe<T> source) {
this.source = source;
this.stacktrace = createStacktrace();
this.assemblyStacktrace = assemblyStacktrace();
}

static String createStacktrace() {
StackTraceElement[] stes = Thread.currentThread().getStackTrace();
static StackTraceElement[] assemblyStacktrace() {
final StackTraceElement[] in = new Exception().getStackTrace();
final List<StackTraceElement> out = new ArrayList<StackTraceElement>(in.length);

StringBuilder sb = new StringBuilder("Assembly trace:");

for (StackTraceElement e : stes) {
for (StackTraceElement e : in) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll open separate PR after this one where I'll move filtering stacktrace to onError. This is a hot path while onError isn't, so we can save some CPU time on filtering for errorless chains.

String row = e.toString();
if (!fullStackTrace) {
if (e.getLineNumber() <= 1) {
Expand Down Expand Up @@ -85,27 +85,41 @@ static String createStacktrace() {
continue;
}
}
sb.append("\n at ").append(row);
out.add(e);
}

return sb.append("\nOriginal exception:").toString();

return out.toArray(new StackTraceElement[out.size()]);
}

static Throwable addAssembly(Throwable original, StackTraceElement[] assemblyStacktrace) {
final StackTraceElement[] originalStacktrace = original.getStackTrace();
final StackTraceElement[] resultingStacktrace = new StackTraceElement[originalStacktrace.length + assemblyStacktrace.length];

System.arraycopy(originalStacktrace, 0, resultingStacktrace, 0, originalStacktrace.length);

System.arraycopy(assemblyStacktrace, 0, resultingStacktrace,
originalStacktrace.length, assemblyStacktrace.length);

original.setStackTrace(resultingStacktrace);

return original;
}

@Override
public void call(Subscriber<? super T> t) {
source.call(new OnAssemblySubscriber<T>(t, stacktrace));
source.call(new OnAssemblySubscriber<T>(t, assemblyStacktrace));
}

static final class OnAssemblySubscriber<T> extends Subscriber<T> {

final Subscriber<? super T> actual;

final String stacktrace;
final StackTraceElement[] assemblyStacktrace;

public OnAssemblySubscriber(Subscriber<? super T> actual, String stacktrace) {
public OnAssemblySubscriber(Subscriber<? super T> actual, StackTraceElement[] assemblyStacktrace) {
super(actual);
this.actual = actual;
this.stacktrace = stacktrace;
this.assemblyStacktrace = assemblyStacktrace;
}

@Override
Expand All @@ -115,8 +129,7 @@ public void onCompleted() {

@Override
public void onError(Throwable e) {
e = new AssemblyStackTraceException(stacktrace, e);
actual.onError(e);
actual.onError(addAssembly(e, assemblyStacktrace));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import rx.*;
import rx.Completable.CompletableSubscriber;
import rx.exceptions.AssemblyStackTraceException;

/**
* Captures the current stack when it is instantiated, makes
Expand All @@ -30,7 +29,7 @@ public final class OnSubscribeOnAssemblyCompletable<T> implements Completable.Co

final Completable.CompletableOnSubscribe source;

final String stacktrace;
final StackTraceElement[] assemblyStacktrace;

/**
* If set to true, the creation of PublisherOnAssembly will capture the raw
Expand All @@ -40,23 +39,23 @@ public final class OnSubscribeOnAssemblyCompletable<T> implements Completable.Co

public OnSubscribeOnAssemblyCompletable(Completable.CompletableOnSubscribe source) {
this.source = source;
this.stacktrace = OnSubscribeOnAssembly.createStacktrace();
this.assemblyStacktrace = OnSubscribeOnAssembly.assemblyStacktrace();
}

@Override
public void call(Completable.CompletableSubscriber t) {
source.call(new OnAssemblyCompletableSubscriber(t, stacktrace));
source.call(new OnAssemblyCompletableSubscriber(t, assemblyStacktrace));
}

static final class OnAssemblyCompletableSubscriber implements CompletableSubscriber {

final CompletableSubscriber actual;

final String stacktrace;
final StackTraceElement[] assemblyStacktrace;

public OnAssemblyCompletableSubscriber(CompletableSubscriber actual, String stacktrace) {
public OnAssemblyCompletableSubscriber(CompletableSubscriber actual, StackTraceElement[] assemblyStacktrace) {
this.actual = actual;
this.stacktrace = stacktrace;
this.assemblyStacktrace = assemblyStacktrace;
}

@Override
Expand All @@ -71,8 +70,7 @@ public void onCompleted() {

@Override
public void onError(Throwable e) {
e = new AssemblyStackTraceException(stacktrace, e);
actual.onError(e);
actual.onError(OnSubscribeOnAssembly.addAssembly(e, assemblyStacktrace));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package rx.internal.operators;

import rx.*;
import rx.exceptions.AssemblyStackTraceException;

/**
* Captures the current stack when it is instantiated, makes
Expand All @@ -29,7 +28,7 @@ public final class OnSubscribeOnAssemblySingle<T> implements Single.OnSubscribe<

final Single.OnSubscribe<T> source;

final String stacktrace;
final StackTraceElement[] assemblyStacktrace;

/**
* If set to true, the creation of PublisherOnAssembly will capture the raw
Expand All @@ -39,30 +38,29 @@ public final class OnSubscribeOnAssemblySingle<T> implements Single.OnSubscribe<

public OnSubscribeOnAssemblySingle(Single.OnSubscribe<T> source) {
this.source = source;
this.stacktrace = OnSubscribeOnAssembly.createStacktrace();
this.assemblyStacktrace = OnSubscribeOnAssembly.assemblyStacktrace();
}

@Override
public void call(SingleSubscriber<? super T> t) {
source.call(new OnAssemblySingleSubscriber<T>(t, stacktrace));
source.call(new OnAssemblySingleSubscriber<T>(t, assemblyStacktrace));
}

static final class OnAssemblySingleSubscriber<T> extends SingleSubscriber<T> {

final SingleSubscriber<? super T> actual;

final String stacktrace;
final StackTraceElement[] assemblyStacktrace;

public OnAssemblySingleSubscriber(SingleSubscriber<? super T> actual, String stacktrace) {
public OnAssemblySingleSubscriber(SingleSubscriber<? super T> actual, StackTraceElement[] assemblyStacktrace) {
this.actual = actual;
this.stacktrace = stacktrace;
this.assemblyStacktrace = assemblyStacktrace;
actual.add(this);
}

@Override
public void onError(Throwable e) {
e = new AssemblyStackTraceException(stacktrace, e);
actual.onError(e);
actual.onError(OnSubscribeOnAssembly.addAssembly(e, assemblyStacktrace));
}

@Override
Expand Down
Loading