Add getStackTrace

This commit is contained in:
Zalmoxisus 2018-12-27 01:10:55 +02:00
parent f18ef4e66e
commit 04673cb032
2 changed files with 31 additions and 1 deletions

View File

@ -28,7 +28,8 @@
"prefer-template": 0, "prefer-template": 0,
"prefer-rest-params": 0, "prefer-rest-params": 0,
"no-proto": 0, "no-proto": 0,
"no-underscore-dangle": 0 "no-underscore-dangle": 0,
"max-len": ["error", { "code": 120 }]
}, },
"plugins": [ "plugins": [
"react" "react"

View File

@ -128,3 +128,32 @@ export function getSeralizeParameter(config, param) {
if (typeof serializeState === 'function') return { replacer: value }; if (typeof serializeState === 'function') return { replacer: value };
return value; return value;
} }
export function getStackTrace(config, toExcludeFromTrace) {
if (!config.trace) return undefined;
if (typeof config.trace === 'function') return config.trace();
let stack;
let extraFrames = 0;
let prevStackTraceLimit;
const traceLimit = config.traceLimit;
const error = Error();
if (Error.captureStackTrace) {
if (Error.stackTraceLimit < traceLimit) {
prevStackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = traceLimit;
}
Error.captureStackTrace(error, toExcludeFromTrace);
} else {
extraFrames = 3;
}
stack = error.stack;
if (prevStackTraceLimit) Error.stackTraceLimit = prevStackTraceLimit;
if (extraFrames || typeof Error.stackTraceLimit !== 'number' || Error.stackTraceLimit > traceLimit) {
const frames = stack.split('\n');
if (frames.length > traceLimit) {
stack = frames.slice(0, traceLimit + extraFrames + (frames[0] === 'Error' ? 1 : 0)).join('\n');
}
}
return stack;
}