Deprecated utilities package for Sentry JavaScript SDKs - all functionality moved to @sentry/core
—
DEPRECATED: Import all functions from @sentry/core instead of @sentry/utils.
Stack trace parsing, manipulation, and filtering utilities for error processing and debugging across different JavaScript environments.
Core functionality for creating customizable stack parsers.
/**
* A function that parses a single line of a stack trace
*/
type StackLineParser = (line: string) => StackFrame | undefined;
/**
* A function that parses a complete stack trace
*/
type StackParser = (stack: string, skipFirst?: number) => StackFrame[];
/**
* Creates a stack parser by combining multiple line parsers
* @param parsers - Array of line parsers to try in order
* @returns Combined stack parser function
*/
function createStackParser(...parsers: StackLineParser[]): StackParser;
/**
* Creates a stack parser from parser configuration options
* @param stackParserOptions - Configuration options for the parser
* @returns Configured stack parser
*/
function stackParserFromStackParserOptions(stackParserOptions: StackParserOptions): StackParser;Pre-built parsers for common JavaScript environments.
/**
* Stack line parser optimized for Node.js stack traces
* @returns Parser function for Node.js format stack lines
*/
function nodeStackLineParser(): StackLineParser;
/**
* Node.js-specific parsing utilities and constants
*/
declare const node: {
/** Standard Node.js stack line parser */
nodeStackLineParser: () => StackLineParser;
/** Additional Node.js parsing utilities */
[key: string]: any;
};Functions for working with parsed stack frames.
interface StackFrame {
/** Source file name */
filename?: string;
/** Function name */
function?: string;
/** Module name */
module?: string;
/** Platform identifier */
platform?: string;
/** Line number */
lineno?: number;
/** Column number */
colno?: number;
/** Absolute file path */
abs_path?: string;
/** Source code line at error */
context_line?: string;
/** Lines before the error */
pre_context?: string[];
/** Lines after the error */
post_context?: string[];
/** Whether frame is from app code (not libraries) */
in_app?: boolean;
/** Memory address (native code) */
instruction_addr?: string;
/** Address mode */
addr_mode?: string;
/** Package name */
package?: string;
/** Symbol name */
symbol?: string;
/** Symbol address */
symbol_addr?: string;
/** Trust level of the frame */
trust?: string;
}
/**
* Extracts stack frames from an event object
* @param event - Event containing exception data
* @returns Array of stack frames or undefined
*/
function getFramesFromEvent(event: Event): StackFrame[] | undefined;
/**
* Parses stack frames from an error object using the provided parser
* @param parser - Stack parser to use
* @param err - Error object with stack trace
* @returns Array of parsed stack frames
*/
function parseStackFrames(parser: StackParser, err: Error & { stacktrace?: string }): StackFrame[];
/**
* Converts a DevTools call frame to a Sentry stack frame
* @param callFrame - DevTools protocol call frame
* @returns Sentry stack frame
*/
function callFrameToStackFrame(callFrame: any): StackFrame;Utilities for filtering and transforming stack traces.
/**
* Removes Sentry-related frames from stack and reverses order
* @param frames - Array of stack frames to process
* @returns Filtered and reversed stack frames
*/
function stripSentryFramesAndReverse(frames: StackFrame[]): StackFrame[];
/**
* Determines if a filename should be marked as "in_app" (application code)
* @param filename - File path to check
* @param inAppIncludes - Patterns for files to include as app code
* @param inAppExcludes - Patterns for files to exclude from app code
* @returns True if filename represents application code
*/
function filenameIsInApp(
filename: string,
inAppIncludes?: Array<string | RegExp>,
inAppExcludes?: Array<string | RegExp>
): boolean;
/**
* Extracts function name from a stack frame
* @param frame - Stack frame to analyze
* @returns Function name or undefined
*/
function getFunctionName(frame: StackFrame): string | undefined;Usage Examples:
import {
createStackParser,
nodeStackLineParser,
parseStackFrames,
stripSentryFramesAndReverse,
filenameIsInApp
} from "@sentry/core";
// Create a parser for Node.js environments
const parser = createStackParser(nodeStackLineParser());
// Parse an error's stack trace
function processError(error: Error) {
const frames = parseStackFrames(parser, error);
// Filter out Sentry internal frames
const cleanFrames = stripSentryFramesAndReverse(frames);
// Mark frames as in-app or library code
const processedFrames = cleanFrames.map(frame => ({
...frame,
in_app: frame.filename ? filenameIsInApp(
frame.filename,
[/^\/app\//, /src\//], // Include app patterns
[/node_modules/, /dist\/vendor/] // Exclude library patterns
) : false
}));
return processedFrames;
}Constants and helper functions for stack processing.
/**
* Constant representing an unknown function name
*/
declare const UNKNOWN_FUNCTION: string;
/**
* Adds source code context to a stack frame
* @param frame - Stack frame to enhance
* @param linesOfContext - Number of context lines to include
* @returns Enhanced frame with context
*/
function addContextToFrame(
frame: StackFrame,
linesOfContext?: number
): StackFrame;Complete pipeline for processing error stack traces:
import {
createStackParser,
nodeStackLineParser,
parseStackFrames,
stripSentryFramesAndReverse,
filenameIsInApp,
addContextToFrame,
getFunctionName
} from "@sentry/core";
class StackProcessor {
private parser: StackParser;
constructor() {
// Create parser with multiple fallbacks
this.parser = createStackParser(
nodeStackLineParser(),
// Could add browser parsers here for universal code
);
}
processError(error: Error): ProcessedStackTrace {
// Parse the raw stack trace
let frames = parseStackFrames(this.parser, error);
// Remove Sentry internal frames and reverse order
frames = stripSentryFramesAndReverse(frames);
// Enhance frames with additional information
frames = frames.map(frame => this.enhanceFrame(frame));
return {
frames,
frameCount: frames.length,
inAppFrameCount: frames.filter(f => f.in_app).length
};
}
private enhanceFrame(frame: StackFrame): StackFrame {
let enhanced = { ...frame };
// Determine if frame is from application code
if (enhanced.filename) {
enhanced.in_app = filenameIsInApp(
enhanced.filename,
[/^\/app\//, /src\//, /lib\/(?!node_modules)/],
[/node_modules/, /vendor/, /dist\/external/]
);
}
// Extract function name if missing
if (!enhanced.function) {
enhanced.function = getFunctionName(enhanced) || UNKNOWN_FUNCTION;
}
// Add source context if available
enhanced = addContextToFrame(enhanced, 5);
return enhanced;
}
}
interface ProcessedStackTrace {
frames: StackFrame[];
frameCount: number;
inAppFrameCount: number;
}Creating custom parsers for specific stack trace formats:
import { createStackParser, StackLineParser } from "@sentry/core";
// Custom parser for a specific logging format
const customLogParser: StackLineParser = (line: string) => {
// Example format: "at functionName (/path/to/file.js:123:45)"
const match = line.match(/^\s*at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)$/);
if (!match) {
return undefined;
}
const [, functionName, filename, lineno, colno] = match;
return {
function: functionName,
filename,
lineno: parseInt(lineno, 10),
colno: parseInt(colno, 10),
in_app: !filename.includes('node_modules')
};
};
// Combine with existing parsers
const hybridParser = createStackParser(
customLogParser,
nodeStackLineParser()
);Advanced filtering based on various criteria:
import { StackFrame } from "@sentry/core";
class StackFilter {
static filterInternalFrames(frames: StackFrame[]): StackFrame[] {
return frames.filter(frame => {
// Remove frames without file information
if (!frame.filename) return false;
// Remove known internal libraries
const internalPatterns = [
/node_modules\/@sentry\//,
/node_modules\/source-map-support/,
/internal\/process/,
/<anonymous>/
];
return !internalPatterns.some(pattern =>
pattern.test(frame.filename!)
);
});
}
static limitFrameCount(frames: StackFrame[], maxFrames: number = 50): StackFrame[] {
if (frames.length <= maxFrames) {
return frames;
}
// Keep top frames and some bottom frames
const topFrames = frames.slice(0, Math.floor(maxFrames * 0.7));
const bottomFrames = frames.slice(-(Math.floor(maxFrames * 0.3)));
return [
...topFrames,
{
function: '... truncated ...',
filename: `... ${frames.length - maxFrames} frames omitted ...`,
lineno: 0,
colno: 0
},
...bottomFrames
];
}
static prioritizeAppFrames(frames: StackFrame[]): StackFrame[] {
const appFrames = frames.filter(f => f.in_app);
const libraryFrames = frames.filter(f => !f.in_app);
// Show app frames first, then most relevant library frames
return [
...appFrames,
...libraryFrames.slice(0, 10) // Limit library frames
];
}
}interface StackParserOptions {
iteratee?: (frames: StackFrame[]) => StackFrame[];
[key: string]: any;
}
interface Exception {
type?: string;
value?: string;
stacktrace?: {
frames?: StackFrame[];
frames_omitted?: [number, number];
};
mechanism?: {
type: string;
handled?: boolean;
[key: string]: any;
};
}Migration Note: All stack processing functions have been moved from @sentry/utils to @sentry/core. Update your imports accordingly.
Install with Tessl CLI
npx tessl i tessl/npm-sentry--utils