Skip to content

Commit

Permalink
Fix stacktrace parsing for JS
Browse files Browse the repository at this point in the history
Reviewed By: adamjernst, indragiek

Differential Revision: D7734756

fbshipit-source-id: 7111932386bb5fede83b5f55a946549b1dc6c402
  • Loading branch information
johnislarry authored and facebook-github-bot committed Apr 24, 2018
1 parent f80000b commit 37d28be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public class StackTraceHelper {
public static final java.lang.String COLUMN_KEY = "column";
public static final java.lang.String LINE_NUMBER_KEY = "lineNumber";

private static final Pattern STACK_FRAME_PATTERN = Pattern.compile(
private static final Pattern STACK_FRAME_PATTERN1 = Pattern.compile(
"^(?:(.*?)@)?(.*?)\\:([0-9]+)\\:([0-9]+)$");
private static final Pattern STACK_FRAME_PATTERN2 = Pattern.compile(
"\\s*(?:at)\\s*(.+?)\\s*[@(](.*):([0-9]+):([0-9]+)[)]$");

/**
* Represents a generic entry in a stack trace, be it originally from JS or Java.
Expand Down Expand Up @@ -175,16 +177,22 @@ public static StackFrame[] convertJsStackTrace(String stack) {
String[] stackTrace = stack.split("\n");
StackFrame[] result = new StackFrame[stackTrace.length];
for (int i = 0; i < stackTrace.length; ++i) {
Matcher matcher = STACK_FRAME_PATTERN.matcher(stackTrace[i]);
if (matcher.find()) {
result[i] = new StackFrameImpl(
matcher.group(2),
matcher.group(1) == null ? "(unknown)" : matcher.group(1),
Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)));
Matcher matcher1 = STACK_FRAME_PATTERN1.matcher(stackTrace[i]);
Matcher matcher2 = STACK_FRAME_PATTERN2.matcher(stackTrace[i]);
Matcher matcher;
if (matcher2.find()) {
matcher = matcher2;
} else if (matcher1.find()) {
matcher = matcher1;
} else {
result[i] = new StackFrameImpl(null, stackTrace[i], -1, -1);
continue;
}
result[i] = new StackFrameImpl(
matcher.group(2),
matcher.group(1) == null ? "(unknown)" : matcher.group(1),
Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
@RunWith(RobolectricTestRunner.class)
public class StackTraceHelperTest {

@Test
public void testParseAlternateFormatStackFrameWithMethod() {
final StackFrame frame = StackTraceHelper.convertJsStackTrace(
"at func1 (/path/to/file.js:2:18)")[0];
assertThat(frame.getMethod()).isEqualTo("func1");
assertThat(frame.getFileName()).isEqualTo("file.js");
assertThat(frame.getLine()).isEqualTo(2);
assertThat(frame.getColumn()).isEqualTo(18);
}

@Test
public void testParseStackFrameWithMethod() {
final StackFrame frame = StackTraceHelper.convertJsStackTrace(
Expand Down

0 comments on commit 37d28be

Please sign in to comment.