Only pass Error.stack to parseErrorStack

Summary:
I want to be able parse error stacks in contexts where I have a call stack string but no error object.

This diff changes parseErrorStack to only accept the stack, instead of a whole error object.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D22752048

fbshipit-source-id: b4b1cd58802eefe736130d48a82bc091241a11ee
This commit is contained in:
Rick Hanlon
2020-07-27 13:57:58 -07:00
committed by Facebook GitHub Bot
parent cd6e2b468d
commit 9edfc43aad
5 changed files with 13 additions and 14 deletions
@@ -59,7 +59,7 @@ class GlobalEvalWithSourceUrlTest extends React.Component<{...}> {
'Expected globalEvalWithSourceUrl to throw an Error object',
);
}
const parsedStack = parseErrorStack(error);
const parsedStack = parseErrorStack(error?.stack);
if (parsedStack[0].file !== url) {
throw new Error(
`Expected first eval stack frame to be in ${url} but found ${String(
@@ -18,7 +18,7 @@ function getFakeError() {
describe('parseErrorStack', function() {
it('parses error stack', function() {
const stack = parseErrorStack(getFakeError());
const stack = parseErrorStack(getFakeError().stack);
expect(stack.length).toBeGreaterThan(0);
const firstFrame = stack[0];
@@ -33,12 +33,12 @@ describe('parseErrorStack', function() {
return error;
}
const stack = parseErrorStack(getWrappedError());
const stack = parseErrorStack(getWrappedError().stack);
expect(stack[0].methodName).toEqual('getFakeError');
});
it('ignores bad inputs', function() {
expect(parseErrorStack({})).toEqual([]);
expect(parseErrorStack(undefined)).toEqual([]);
expect(parseErrorStack(null)).toEqual([]);
});
});
+7 -7
View File
@@ -47,22 +47,22 @@ function convertHermesStack(stack: HermesParsedStack): Array<StackFrame> {
return frames;
}
function parseErrorStack(e: ExtendedError): Array<StackFrame> {
if (!e || !e.stack) {
function parseErrorStack(errorStack?: string): Array<StackFrame> {
if (errorStack == null) {
return [];
}
const stacktraceParser = require('stacktrace-parser');
const stack = Array.isArray(e.stack)
? e.stack
const parsedStack = Array.isArray(errorStack)
? errorStack
: global.HermesInternal
? convertHermesStack(parseHermesStack(e.stack))
: stacktraceParser.parse(e.stack).map(frame => ({
? convertHermesStack(parseHermesStack(errorStack))
: stacktraceParser.parse(errorStack).map(frame => ({
...frame,
column: frame.column != null ? frame.column - 1 : null,
}));
return stack;
return parsedStack;
}
module.exports = parseErrorStack;
+1 -1
View File
@@ -59,7 +59,7 @@ function reportException(
const NativeExceptionsManager = require('./NativeExceptionsManager').default;
if (NativeExceptionsManager) {
const parseErrorStack = require('./Devtools/parseErrorStack');
const stack = parseErrorStack(e);
const stack = parseErrorStack(e?.stack);
const currentExceptionID = ++exceptionID;
const originalMessage = e.message || '';
let message = originalMessage;
+1 -2
View File
@@ -198,8 +198,7 @@ export function addLog(log: LogData): void {
// otherwise spammy logs would pause rendering.
setImmediate(() => {
try {
// TODO: Use Error.captureStackTrace on Hermes
const stack = parseErrorStack(errorForStackTrace);
const stack = parseErrorStack(errorForStackTrace?.stack);
appendNewLog(
new LogBoxLog({