Skip to content

Commit

Permalink
Gracefully handle any unrecognized stack frame format
Browse files Browse the repository at this point in the history
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 9399379
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
Expand Up @@ -9,18 +9,15 @@


package com.facebook.react.devsupport; 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.ReadableArray;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType; import com.facebook.react.bridge.ReadableType;
import com.facebook.react.common.MapBuilder; import com.facebook.react.common.MapBuilder;
import com.facebook.react.devsupport.interfaces.StackFrame; 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
Expand Down Expand Up @@ -180,19 +177,15 @@ public static StackFrame[] convertJsStackTrace(String stack) {
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) {
if (stackTrace[i].equals("[native code]")) { Matcher matcher = STACK_FRAME_PATTERN.matcher(stackTrace[i]);
result[i] = new StackFrameImpl(null, stackTrace[i], -1, -1); if (matcher.find()) {
} else {
Matcher matcher = STACK_FRAME_PATTERN.matcher(stackTrace[i]);
if (!matcher.find()) {
throw new IllegalArgumentException(
"Unexpected stack frame format: " + stackTrace[i]);
}
result[i] = new StackFrameImpl( result[i] = new StackFrameImpl(
matcher.group(2), matcher.group(2),
matcher.group(1) == null ? "(unknown)" : matcher.group(1), matcher.group(1) == null ? "(unknown)" : matcher.group(1),
Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4))); Integer.parseInt(matcher.group(4)));
} else {
result[i] = new StackFrameImpl(null, stackTrace[i], -1, -1);
} }
} }
return result; return result;
Expand Down
Expand Up @@ -9,15 +9,13 @@


package com.facebook.react.devsupport; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;


import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.failBecauseExceptionWasNotThrown;

@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class StackTraceHelperTest { public class StackTraceHelperTest {


Expand All @@ -43,11 +41,19 @@ public void testParseStackFrameWithoutMethod() {


@Test @Test
public void testParseStackFrameWithInvalidFrame() { public void testParseStackFrameWithInvalidFrame() {
try { final StackFrame frame = StackTraceHelper.convertJsStackTrace("Test.bundle:ten:twenty")[0];
StackTraceHelper.convertJsStackTrace("Test.bundle:ten:twenty"); assertThat(frame.getMethod()).isEqualTo("Test.bundle:ten:twenty");
failBecauseExceptionWasNotThrown(IllegalArgumentException.class); assertThat(frame.getFileName()).isEqualTo("");
} catch (Exception e) { assertThat(frame.getLine()).isEqualTo(-1);
assertThat(e).isInstanceOf(IllegalArgumentException.class); 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.