Don't crash in StackTraceHelper.convertJsStackTrace
Summary: Don't crash in `StackTraceHelper.convertJsStackTrace` on missing `lineNumber` I've seen the native Android app crash upon displaying a stack trace because of this bug. Incidental and unrelated change: Refactor `StackTraceHelper.formatFrameSource` to use `StringBuilder` Closes https://github.com/facebook/react-native/pull/12920 Differential Revision: D4709273 fbshipit-source-id: 95b3eb303f259460e33d8ee32a2db652360c645d
This commit is contained in:
parent
87137cadd7
commit
957b55c769
|
@ -22,6 +22,9 @@ import com.facebook.react.devsupport.interfaces.StackFrame;
|
||||||
*/
|
*/
|
||||||
public class StackTraceHelper {
|
public class StackTraceHelper {
|
||||||
|
|
||||||
|
public static final java.lang.String COLUMN_KEY = "column";
|
||||||
|
public static final java.lang.String LINE_NUMBER_KEY = "lineNumber";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
|
@ -101,10 +104,13 @@ public class StackTraceHelper {
|
||||||
ReadableMap frame = stack.getMap(i);
|
ReadableMap frame = stack.getMap(i);
|
||||||
String methodName = frame.getString("methodName");
|
String methodName = frame.getString("methodName");
|
||||||
String fileName = frame.getString("file");
|
String fileName = frame.getString("file");
|
||||||
int lineNumber = frame.getInt("lineNumber");
|
int lineNumber = -1;
|
||||||
|
if (frame.hasKey(LINE_NUMBER_KEY) && !frame.isNull(LINE_NUMBER_KEY)) {
|
||||||
|
lineNumber = frame.getInt(LINE_NUMBER_KEY);
|
||||||
|
}
|
||||||
int columnNumber = -1;
|
int columnNumber = -1;
|
||||||
if (frame.hasKey("column") && !frame.isNull("column")) {
|
if (frame.hasKey(COLUMN_KEY) && !frame.isNull(COLUMN_KEY)) {
|
||||||
columnNumber = frame.getInt("column");
|
columnNumber = frame.getInt(COLUMN_KEY);
|
||||||
}
|
}
|
||||||
result[i] = new StackFrameImpl(fileName, methodName, lineNumber, columnNumber);
|
result[i] = new StackFrameImpl(fileName, methodName, lineNumber, columnNumber);
|
||||||
}
|
}
|
||||||
|
@ -132,12 +138,17 @@ public class StackTraceHelper {
|
||||||
* Format a {@link StackFrame} to a String (method name is not included).
|
* Format a {@link StackFrame} to a String (method name is not included).
|
||||||
*/
|
*/
|
||||||
public static String formatFrameSource(StackFrame frame) {
|
public static String formatFrameSource(StackFrame frame) {
|
||||||
String lineInfo = "";
|
StringBuilder lineInfo = new StringBuilder();
|
||||||
|
lineInfo.append(frame.getFileName());
|
||||||
|
final int line = frame.getLine();
|
||||||
|
if (line > 0) {
|
||||||
|
lineInfo.append(":").append(line);
|
||||||
final int column = frame.getColumn();
|
final int column = frame.getColumn();
|
||||||
// If the column is 0, don't show it in red box.
|
if (column > 0) {
|
||||||
final String columnString = column <= 0 ? "" : ":" + column;
|
lineInfo.append(":").append(column);
|
||||||
lineInfo += frame.getFileName() + ":" + frame.getLine() + columnString;
|
}
|
||||||
return lineInfo;
|
}
|
||||||
|
return lineInfo.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue