Permalink
Browse files

Gracefully handle any unrecognized stack frame format

Reviewed By: javache

Differential Revision: D5736692

fbshipit-source-id: e90dc38fa203ae65ac839f37940e96f23b35c330
  • Loading branch information...
adamjernst authored and facebook-github-bot committed Aug 31, 2017
1 parent 259161f commit 93993799eba2885c1d6aac6aaa8f015f2b7171a7
@@ -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 JSONObject toJSON() {
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]);
}
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)));
} else {
result[i] = new StackFrameImpl(null, stackTrace[i], -1, -1);
}
}
return result;
@@ -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 void testParseStackFrameWithoutMethod() {
@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);
}
}

0 comments on commit 9399379

Please sign in to comment.