Fix stacktrace parsing for JS

Reviewed By: adamjernst, indragiek

Differential Revision: D7734756

fbshipit-source-id: 7111932386bb5fede83b5f55a946549b1dc6c402
This commit is contained in:
Max Sherman 2018-04-23 22:05:32 -07:00 committed by Facebook Github Bot
parent f80000b9e7
commit 37d28be2d9
2 changed files with 26 additions and 8 deletions

View File

@ -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.
@ -175,16 +177,22 @@ public class StackTraceHelper {
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;
}

View File

@ -17,6 +17,16 @@ import org.robolectric.RobolectricTestRunner;
@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(