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 COLUMN_KEY = "column";
public static final java.lang.String LINE_NUMBER_KEY = "lineNumber"; 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]+)$"); "^(?:(.*?)@)?(.*?)\\:([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. * 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"); String[] stackTrace = stack.split("\n");
StackFrame[] result = new StackFrame[stackTrace.length]; StackFrame[] result = new StackFrame[stackTrace.length];
for (int i = 0; i < stackTrace.length; ++i) { for (int i = 0; i < stackTrace.length; ++i) {
Matcher matcher = STACK_FRAME_PATTERN.matcher(stackTrace[i]); Matcher matcher1 = STACK_FRAME_PATTERN1.matcher(stackTrace[i]);
if (matcher.find()) { Matcher matcher2 = STACK_FRAME_PATTERN2.matcher(stackTrace[i]);
result[i] = new StackFrameImpl( Matcher matcher;
matcher.group(2), if (matcher2.find()) {
matcher.group(1) == null ? "(unknown)" : matcher.group(1), matcher = matcher2;
Integer.parseInt(matcher.group(3)), } else if (matcher1.find()) {
Integer.parseInt(matcher.group(4))); matcher = matcher1;
} else { } else {
result[i] = new StackFrameImpl(null, stackTrace[i], -1, -1); 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; return result;
} }

View File

@ -17,6 +17,16 @@ import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class StackTraceHelperTest { 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 @Test
public void testParseStackFrameWithMethod() { public void testParseStackFrameWithMethod() {
final StackFrame frame = StackTraceHelper.convertJsStackTrace( final StackFrame frame = StackTraceHelper.convertJsStackTrace(