Skip to content

Commit 7d58810

Browse files
author
Mikel Pascual
committed
Allow configurable list of base package names and avoid adding root cause if there already was a meaningful stacktrace
1 parent 66bae8e commit 7d58810

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/main/java/com/akaita/java/rxjava2debug/ExceptionUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
package com.akaita.java.rxjava2debug;
1818

1919
import io.reactivex.annotations.NonNull;
20+
import io.reactivex.annotations.Nullable;
2021

2122
import java.util.LinkedList;
2223
import java.util.List;
2324

2425
class ExceptionUtils {
2526

26-
static Throwable setRootCause(@NonNull Throwable throwable, @NonNull Throwable rootCause) {
27+
static Throwable setRootCause(@NonNull Throwable throwable, @NonNull Throwable rootCause, @Nullable String[] basePackages) {
2728
if (throwable == null){
2829
return null;
2930
}
@@ -32,8 +33,13 @@ static Throwable setRootCause(@NonNull Throwable throwable, @NonNull Throwable r
3233
}
3334

3435
List<Throwable> causes = listCauses(throwable);
35-
causes.add(rootCause);
36-
return collapseCauses(causes);
36+
if (basePackages != null
37+
&& StackTraceUtils.anyContains(causes, basePackages)) {
38+
return throwable;
39+
} else {
40+
causes.add(rootCause);
41+
return collapseCauses(causes);
42+
}
3743
}
3844

3945
private static @NonNull

src/main/java/com/akaita/java/rxjava2debug/RxJava2Debug.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@
1818

1919
import hu.akarnokd.rxjava2.debug.RxJavaAssemblyException;
2020
import hu.akarnokd.rxjava2.debug.RxJavaAssemblyTracking;
21+
import io.reactivex.annotations.NonNull;
22+
import io.reactivex.annotations.Nullable;
23+
24+
import java.util.List;
2125

2226
import static com.akaita.java.rxjava2debug.ExceptionUtils.setRootCause;
2327
import static com.akaita.java.rxjava2debug.StackTraceUtils.parseStackTrace;
2428

2529
public class RxJava2Debug {
26-
private static String BASE_APP_PACKAGE_NAME = "com.akaita.fgas";
30+
private @Nullable
31+
static String[] basePackages;
2732

2833
/**
2934
* Enable a system to collect information about RxJava's execution to provide a more meaningful StackTrace in case of crash<br/>
3035
* <b>Beware:</b> Any crash-reporting handler should be set up <i>before</i> calling this method
36+
* @param basePackageNames List of base package names of your code, so the created stacktrace will have one of those on its top<br/>
37+
* <i>null</i> to disable any filtering
3138
*/
32-
public static void enableRxJava2AssemblyTracking() {
39+
public static void enableRxJava2AssemblyTracking(@Nullable String[] basePackageNames) {
40+
basePackages = basePackageNames;
3341
RxJavaAssemblyTracking.enable();
3442
setRxJavaAssemblyHandler();
3543
}
@@ -54,10 +62,10 @@ public void uncaughtException(Thread t, Throwable e) {
5462

5563
RxJavaAssemblyException assembledException = RxJavaAssemblyException.find(e);
5664
if (assembledException != null) {
57-
StackTraceElement[] clearStack = parseStackTrace(assembledException, BASE_APP_PACKAGE_NAME);
65+
StackTraceElement[] clearStack = parseStackTrace(assembledException, basePackages);
5866
Throwable clearException = new Throwable();
5967
clearException.setStackTrace(clearStack);
60-
toThrow = setRootCause(e, clearException);
68+
toThrow = setRootCause(e, clearException, basePackages);
6169
}
6270

6371
previousDefaultHandler.uncaughtException(t, toThrow);

src/main/java/com/akaita/java/rxjava2debug/StackTraceUtils.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ class StackTraceUtils {
3737
* @return StackTrace, filtered so a app-specific line is at the top of it
3838
*/
3939
@NonNull
40-
static StackTraceElement[] parseStackTrace(@NonNull RxJavaAssemblyException exception, @NonNull String basePackageName) {
40+
static StackTraceElement[] parseStackTrace(@NonNull RxJavaAssemblyException exception, @Nullable String[] basePackages) {
4141
String[] lines = exception.stacktrace()
4242
.split(NEW_LINE_REGEX);
4343

4444
List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
45-
boolean appSpecificFound = false;
45+
boolean filterIn = false;
4646
for (String line : lines) {
47-
appSpecificFound = appSpecificFound || line.contains(basePackageName);
48-
if (appSpecificFound) {
47+
filterIn = filterIn
48+
|| basePackages == null
49+
|| startsWithAny(line, basePackages);
50+
if (filterIn) {
4951
StackTraceElement element = parseStackTraceLine(line);
5052
if (element != null) {
5153
stackTrace.add(element);
@@ -56,6 +58,25 @@ static StackTraceElement[] parseStackTrace(@NonNull RxJavaAssemblyException exce
5658
return stackTrace.toArray(new StackTraceElement[0]);
5759
}
5860

61+
static boolean startsWithAny(@NonNull String input, @NonNull String[] matchers) {
62+
for (String matcher :
63+
matchers) {
64+
if (input.startsWith("at "+matcher) || input.startsWith(matcher)) return true;
65+
}
66+
return false;
67+
}
68+
69+
static boolean anyContains(List<Throwable> causes, @NonNull String[] basePackages) {
70+
for (Throwable cause : causes) {
71+
StackTraceElement[] stackTrace = cause.getStackTrace();
72+
for (StackTraceElement element : stackTrace) {
73+
if (startsWithAny(element.getClassName(), basePackages)) return true;
74+
}
75+
}
76+
77+
return false;
78+
}
79+
5980
/**
6081
* Parse string containing a <i>single line</i> of a StackTrace
6182
*

0 commit comments

Comments
 (0)