2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2015-03-20 15:43:51 +00:00
|
|
|
* @providesModule parseErrorStack
|
2016-06-01 20:50:34 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-06-01 20:50:34 +00:00
|
|
|
export type StackFrame = {
|
2017-02-27 10:33:49 +00:00
|
|
|
column: ?number,
|
2016-08-09 13:32:41 +00:00
|
|
|
file: string,
|
|
|
|
lineNumber: number,
|
2017-02-27 10:33:49 +00:00
|
|
|
methodName: string,
|
2016-06-01 20:50:34 +00:00
|
|
|
};
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
export type ExtendedError = Error & {
|
|
|
|
framesToPop?: number,
|
|
|
|
};
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
function parseErrorStack(e: ExtendedError): Array<StackFrame> {
|
2015-06-22 17:08:20 +00:00
|
|
|
if (!e || !e.stack) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
const stacktraceParser = require('stacktrace-parser');
|
|
|
|
const stack = Array.isArray(e.stack) ? e.stack : stacktraceParser.parse(e.stack);
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
let framesToPop = typeof e.framesToPop === 'number' ? e.framesToPop : 0;
|
2015-01-30 01:10:49 +00:00
|
|
|
while (framesToPop--) {
|
|
|
|
stack.shift();
|
|
|
|
}
|
2015-03-20 15:43:51 +00:00
|
|
|
return stack;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 15:43:51 +00:00
|
|
|
module.exports = parseErrorStack;
|