2015-01-29 17:10:49 -08:00
|
|
|
/**
|
2015-03-23 15:07:33 -07:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-16 18:24:55 -08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-01-29 17:10:49 -08:00
|
|
|
*
|
2015-03-20 08:43:51 -07:00
|
|
|
* @providesModule parseErrorStack
|
2016-06-01 13:50:34 -07:00
|
|
|
* @flow
|
2015-01-29 17:10:49 -08:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-06-01 13:50:34 -07:00
|
|
|
export type StackFrame = {
|
2017-02-27 02:33:49 -08:00
|
|
|
column: ?number,
|
2016-08-09 06:32:41 -07:00
|
|
|
file: string,
|
|
|
|
lineNumber: number,
|
2017-02-27 02:33:49 -08:00
|
|
|
methodName: string,
|
2016-06-01 13:50:34 -07:00
|
|
|
};
|
2015-01-29 17:10:49 -08:00
|
|
|
|
2017-06-14 09:16:26 -07:00
|
|
|
export type ExtendedError = Error & {
|
|
|
|
framesToPop?: number,
|
|
|
|
};
|
2015-01-29 17:10:49 -08:00
|
|
|
|
2017-06-14 09:16:26 -07:00
|
|
|
function parseErrorStack(e: ExtendedError): Array<StackFrame> {
|
2015-06-22 10:08:20 -07:00
|
|
|
if (!e || !e.stack) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-09-06 03:25:01 -07:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an
|
|
|
|
* error found when Flow v0.54 was deployed. To see the error delete this
|
|
|
|
* comment and run Flow. */
|
2017-06-14 09:16:26 -07:00
|
|
|
const stacktraceParser = require('stacktrace-parser');
|
|
|
|
const stack = Array.isArray(e.stack) ? e.stack : stacktraceParser.parse(e.stack);
|
2015-01-29 17:10:49 -08:00
|
|
|
|
2017-06-14 09:16:26 -07:00
|
|
|
let framesToPop = typeof e.framesToPop === 'number' ? e.framesToPop : 0;
|
2015-01-29 17:10:49 -08:00
|
|
|
while (framesToPop--) {
|
|
|
|
stack.shift();
|
|
|
|
}
|
2015-03-20 08:43:51 -07:00
|
|
|
return stack;
|
2015-01-29 17:10:49 -08:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:43:51 -07:00
|
|
|
module.exports = parseErrorStack;
|