Gracefully handle any unrecognized stack frame format

Reviewed By: javache

Differential Revision: D5736692

fbshipit-source-id: e90dc38fa203ae65ac839f37940e96f23b35c330
This commit is contained in:
Adam Ernst 2017-08-31 07:34:06 -07:00 committed by Facebook Github Bot
parent 259161f872
commit 93993799eb
2 changed files with 24 additions and 25 deletions

View File

@ -9,18 +9,15 @@
package com.facebook.react.devsupport;
import javax.annotation.Nullable;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.devsupport.interfaces.StackFrame;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -180,19 +177,15 @@ public class StackTraceHelper {
String[] stackTrace = stack.split("\n");
StackFrame[] result = new StackFrame[stackTrace.length];
for (int i = 0; i < stackTrace.length; ++i) {
if (stackTrace[i].equals("[native code]")) {
result[i] = new StackFrameImpl(null, stackTrace[i], -1, -1);
} else {
Matcher matcher = STACK_FRAME_PATTERN.matcher(stackTrace[i]);
if (!matcher.find()) {
throw new IllegalArgumentException(
"Unexpected stack frame format: " + 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)));
} else {
result[i] = new StackFrameImpl(null, stackTrace[i], -1, -1);
}
}
return result;

View File

@ -9,15 +9,13 @@
package com.facebook.react.devsupport;
import com.facebook.react.devsupport.interfaces.StackFrame;
import static org.fest.assertions.api.Assertions.assertThat;
import com.facebook.react.devsupport.interfaces.StackFrame;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.failBecauseExceptionWasNotThrown;
@RunWith(RobolectricTestRunner.class)
public class StackTraceHelperTest {
@ -43,11 +41,19 @@ public class StackTraceHelperTest {
@Test
public void testParseStackFrameWithInvalidFrame() {
try {
StackTraceHelper.convertJsStackTrace("Test.bundle:ten:twenty");
failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
final StackFrame frame = StackTraceHelper.convertJsStackTrace("Test.bundle:ten:twenty")[0];
assertThat(frame.getMethod()).isEqualTo("Test.bundle:ten:twenty");
assertThat(frame.getFileName()).isEqualTo("");
assertThat(frame.getLine()).isEqualTo(-1);
assertThat(frame.getColumn()).isEqualTo(-1);
}
@Test
public void testParseStackFrameWithNativeCodeFrame() {
final StackFrame frame = StackTraceHelper.convertJsStackTrace("forEach@[native code]")[0];
assertThat(frame.getMethod()).isEqualTo("forEach@[native code]");
assertThat(frame.getFileName()).isEqualTo("");
assertThat(frame.getLine()).isEqualTo(-1);
assertThat(frame.getColumn()).isEqualTo(-1);
}
}