2015-01-30 04:10:49 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-24 01:07:33 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03: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-30 04:10:49 +03:00
|
|
|
*
|
2018-05-11 05:06:46 +03:00
|
|
|
* @format
|
2016-06-01 23:50:34 +03:00
|
|
|
* @flow
|
2015-01-30 04:10:49 +03:00
|
|
|
*/
|
2018-05-11 05:06:46 +03:00
|
|
|
|
2015-01-30 04:10:49 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-01 23:50:34 +03:00
|
|
|
export type StackFrame = {
|
2017-02-27 13:33:49 +03:00
|
|
|
column: ?number,
|
2016-08-09 16:32:41 +03:00
|
|
|
file: string,
|
|
|
|
lineNumber: number,
|
2017-02-27 13:33:49 +03:00
|
|
|
methodName: string,
|
2016-06-01 23:50:34 +03:00
|
|
|
};
|
2015-01-30 04:10:49 +03:00
|
|
|
|
2017-06-14 19:16:26 +03:00
|
|
|
export type ExtendedError = Error & {
|
|
|
|
framesToPop?: number,
|
2018-09-12 23:55:58 +03:00
|
|
|
jsEngine?: string,
|
2017-06-14 19:16:26 +03:00
|
|
|
};
|
2015-01-30 04:10:49 +03:00
|
|
|
|
2017-06-14 19:16:26 +03:00
|
|
|
function parseErrorStack(e: ExtendedError): Array<StackFrame> {
|
2015-06-22 20:08:20 +03:00
|
|
|
if (!e || !e.stack) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-14 19:16:26 +03:00
|
|
|
const stacktraceParser = require('stacktrace-parser');
|
2018-05-11 05:06:46 +03:00
|
|
|
const stack = Array.isArray(e.stack)
|
|
|
|
? e.stack
|
|
|
|
: stacktraceParser.parse(e.stack);
|
2015-01-30 04:10:49 +03:00
|
|
|
|
2017-06-14 19:16:26 +03:00
|
|
|
let framesToPop = typeof e.framesToPop === 'number' ? e.framesToPop : 0;
|
2015-01-30 04:10:49 +03:00
|
|
|
while (framesToPop--) {
|
|
|
|
stack.shift();
|
|
|
|
}
|
2015-03-20 18:43:51 +03:00
|
|
|
return stack;
|
2015-01-30 04:10:49 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:43:51 +03:00
|
|
|
module.exports = parseErrorStack;
|