UNPKG

@sentry/core

Version:
108 lines (107 loc) • 3.73 kB
// src/utils/stacktrace.ts const STACKTRACE_FRAME_LIMIT = 50; const UNKNOWN_FUNCTION = "?"; const WEBPACK_ERROR_REGEXP = /\(error: (.*)\)/; const STRIP_FRAME_REGEXP = /captureMessage|captureException/; /** * Creates a stack parser with the supplied line parsers * * StackFrames are returned in the correct order for Sentry Exception * frames and with Sentry SDK internal frames removed from the top and bottom * */ function createStackParser(...parsers) { const sortedParsers = parsers.sort((a, b) => a[0] - b[0]).map((p) => p[1]); return (stack, skipFirstLines = 0, framesToPop = 0) => { const frames = []; const lines = stack.split("\n"); for (let i = skipFirstLines; i < lines.length; i++) { let line = lines[i]; if (line.length > 1024) line = line.slice(0, 1024); const cleanedLine = WEBPACK_ERROR_REGEXP.test(line) ? line.replace(WEBPACK_ERROR_REGEXP, "$1") : line; if (cleanedLine.includes("Error: ")) continue; for (const parser of sortedParsers) { const frame = parser(cleanedLine); if (frame) { frames.push(frame); break; } } if (frames.length >= STACKTRACE_FRAME_LIMIT + framesToPop) break; } return stripSentryFramesAndReverse(frames.slice(framesToPop)); }; } /** * Gets a stack parser implementation from Options.stackParser * @see Options * * If options contains an array of line parsers, it is converted into a parser */ function stackParserFromStackParserOptions(stackParser) { if (Array.isArray(stackParser)) return createStackParser(...stackParser); return stackParser; } /** * Removes Sentry frames from the top and bottom of the stack if present and enforces a limit of max number of frames. * Assumes stack input is ordered from top to bottom and returns the reverse representation so call site of the * function that caused the crash is the last frame in the array. * @hidden */ function stripSentryFramesAndReverse(stack) { if (!stack.length) return []; const localStack = Array.from(stack); if (/sentryWrapped/.test(getLastStackFrame(localStack).function || "")) localStack.pop(); localStack.reverse(); if (STRIP_FRAME_REGEXP.test(getLastStackFrame(localStack).function || "")) { localStack.pop(); if (STRIP_FRAME_REGEXP.test(getLastStackFrame(localStack).function || "")) localStack.pop(); } return localStack.slice(0, STACKTRACE_FRAME_LIMIT).map((frame) => ({ ...frame, filename: frame.filename || getLastStackFrame(localStack).filename, function: frame.function || "?" })); } function getLastStackFrame(arr) { return arr[arr.length - 1] || {}; } const defaultFunctionName = "<anonymous>"; /** * Safely extract function name from itself */ function getFunctionName(fn) { try { if (!fn || typeof fn !== "function") return defaultFunctionName; return fn.name || defaultFunctionName; } catch { return defaultFunctionName; } } /** * Get's stack frames from an event without needing to check for undefined properties. */ function getFramesFromEvent(event) { const exception = event.exception; if (exception) { const frames = []; try { exception.values.forEach((value) => { if (value.stacktrace.frames) frames.push(...value.stacktrace.frames); }); return frames; } catch { return; } } } /** * Normalizes stack line paths by removing file:// prefix and leading slashes for Windows paths */ function normalizeStackTracePath(path) { let filename = path?.startsWith("file://") ? path.slice(7) : path; if (filename?.match(/\/[A-Z]:/)) filename = filename.slice(1); return filename; } export { UNKNOWN_FUNCTION, createStackParser, getFramesFromEvent, getFunctionName, normalizeStackTracePath, stackParserFromStackParserOptions, stripSentryFramesAndReverse }; //# sourceMappingURL=stacktrace.js.map