UNPKG

@opra/common

Version:
48 lines (47 loc) 1.87 kB
const PATH_PATTERN = /^(?:file:\/\/)?(.+)$/; export function getStackFileName(position = 1) { if (position >= Error.stackTraceLimit) { throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' + position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`'); } const oldPrepareStackTrace = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack) => stack; const stack = new Error().stack; Error.prepareStackTrace = oldPrepareStackTrace; if (stack !== null && typeof stack === 'object') { // stack[0] holds this file // stack[1] holds where this function was called const s = stack[position] ? stack[position].getFileName() : undefined; const m = s ? PATH_PATTERN.exec(s) : undefined; return m ? m[1] : ''; } return ''; } export function getErrorStack(position = 1) { if (position >= Error.stackTraceLimit) { throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' + position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`'); } const oldPrepareStackTrace = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack) => stack; const stack = new Error().stack; Error.prepareStackTrace = oldPrepareStackTrace; if (stack !== null && typeof stack === 'object') { // stack[0] holds this file // stack[1] holds where this function was called const s = stack[position] ? stack[position].getFileName() : undefined; const m = s ? PATH_PATTERN.exec(s) : undefined; return m ? m[1] : ''; } return ''; }