Skip to content

fix(stacktrace): Always use ? for anonymous function name #10732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { captureEvent, defineIntegration, getClient } from '@sentry/core';
import type { Client, Event, IntegrationFn, Primitive, StackParser } from '@sentry/types';
import {
UNKNOWN_FUNCTION,
addGlobalErrorInstrumentationHandler,
addGlobalUnhandledRejectionInstrumentationHandler,
getLocationHref,
Expand Down Expand Up @@ -172,7 +173,7 @@ function _enhanceEventWithInitialFrame(event: Event, url: any, line: any, column
ev0sf.push({
colno,
filename,
function: '?',
function: UNKNOWN_FUNCTION,
in_app: true,
lineno,
});
Expand Down
7 changes: 2 additions & 5 deletions packages/browser/src/stack-parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import type { StackFrame, StackLineParser, StackLineParserFn } from '@sentry/types';
import { createStackParser } from '@sentry/utils';

// global reference to slice
const UNKNOWN_FUNCTION = '?';
import { UNKNOWN_FUNCTION, createStackParser } from '@sentry/utils';

const OPERA10_PRIORITY = 10;
const OPERA11_PRIORITY = 20;
Expand All @@ -38,7 +35,7 @@ const GECKO_PRIORITY = 50;
function createFrame(filename: string, func: string, lineno?: number, colno?: number): StackFrame {
const frame: StackFrame = {
filename,
function: func,
function: func === '<anonymous>' ? UNKNOWN_FUNCTION : func,
in_app: true, // All browser frames are considered in_app
};

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/test/unit/tracekit/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Tracekit - Misc Tests', () => {
{ filename: '<anonymous>', function: 'Array.forEach', in_app: true },
{
filename: '../node_modules/@sentry-internal/rrweb/es/rrweb/ext/@xstate/fsm/es/index.js',
function: '<anonymous>',
function: '?',
in_app: true,
lineno: 15,
colno: 2595,
Expand Down
2 changes: 1 addition & 1 deletion packages/deno/test/__snapshots__/mod.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ snapshot[`captureException 1`] = `
colno: 27,
context_line: " client.captureException(something());",
filename: "app:///test/mod.test.ts",
function: "<anonymous>",
function: "?",
in_app: true,
lineno: 47,
post_context: [
Expand Down
6 changes: 3 additions & 3 deletions packages/node/test/stacktrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('Stack parsing', () => {
{
filename: '/Users/felix/code/node-fast-or-slow/lib/test_case.js',
module: 'test_case',
function: '<anonymous>',
function: '?',
lineno: 80,
colno: 10,
in_app: true,
Expand All @@ -213,7 +213,7 @@ describe('Stack parsing', () => {
{
filename: '/Users/felix/code/node-fast-or-slow/lib/test_case.js',
module: 'test_case',
function: '<anonymous>',
function: '?',
lineno: 80,
colno: 10,
in_app: true,
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('Stack parsing', () => {
{
filename: '/code/node_modules/kafkajs/src/consumer/runner.js',
module: 'kafkajs.src.consumer:runner',
function: '<anonymous>',
function: '?',
lineno: 376,
colno: 15,
in_app: false,
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/anr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StackFrame } from '@sentry/types';

import { filenameIsInApp } from './node-stack-trace';
import { dropUndefinedKeys } from './object';
import { filenameIsInApp } from './stacktrace';
import { UNKNOWN_FUNCTION } from './stacktrace';

type WatchdogReturn = {
/** Resets the watchdog timer */
Expand Down Expand Up @@ -84,7 +84,7 @@ export function callFrameToStackFrame(
return dropUndefinedKeys({
filename,
module: getModuleFromFilename(filename),
function: frame.functionName || '?',
function: frame.functionName || UNKNOWN_FUNCTION,
colno,
lineno,
in_app: filename ? filenameIsInApp(filename) : undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './promisebuffer';
export * from './requestdata';
export * from './severity';
export * from './stacktrace';
export * from './node-stack-trace';
export * from './string';
export * from './supports';
export * from './syncpromise';
Expand Down
15 changes: 13 additions & 2 deletions packages/utils/src/node-stack-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import type { StackLineParserFn } from '@sentry/types';
import type { StackLineParser, StackLineParserFn } from '@sentry/types';
import { UNKNOWN_FUNCTION } from './stacktrace';

export type GetModuleFn = (filename: string | undefined) => string | undefined;

Expand Down Expand Up @@ -96,7 +97,7 @@ export function node(getModule?: GetModuleFn): StackLineParserFn {
}

if (functionName === undefined) {
methodName = methodName || '<anonymous>';
methodName = methodName || UNKNOWN_FUNCTION;
functionName = typeName ? `${typeName}.${methodName}` : methodName;
}

Expand Down Expand Up @@ -131,3 +132,13 @@ export function node(getModule?: GetModuleFn): StackLineParserFn {
return undefined;
};
}

/**
* Node.js stack line parser
*
* This is in @sentry/utils so it can be used from the Electron SDK in the browser for when `nodeIntegration == true`.
* This allows it to be used without referencing or importing any node specific code which causes bundlers to complain
*/
export function nodeStackLineParser(getModule?: GetModuleFn): StackLineParser {
return [90, node(getModule)];
}
18 changes: 2 additions & 16 deletions packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import type { StackFrame, StackLineParser, StackParser } from '@sentry/types';

import type { GetModuleFn } from './node-stack-trace';
import { filenameIsInApp, node } from './node-stack-trace';

export { filenameIsInApp };

const STACKTRACE_FRAME_LIMIT = 50;
export const UNKNOWN_FUNCTION = '?';
// Used to sanitize webpack (error: *) wrapped stack errors
const WEBPACK_ERROR_REGEXP = /\(error: (.*)\)/;
const STRIP_FRAME_REGEXP = /captureMessage|captureException/;
Expand Down Expand Up @@ -116,7 +112,7 @@ export function stripSentryFramesAndReverse(stack: ReadonlyArray<StackFrame>): S
return localStack.slice(0, STACKTRACE_FRAME_LIMIT).map(frame => ({
...frame,
filename: frame.filename || localStack[localStack.length - 1].filename,
function: frame.function || '?',
function: frame.function || UNKNOWN_FUNCTION,
}));
}

Expand All @@ -137,13 +133,3 @@ export function getFunctionName(fn: unknown): string {
return defaultFunctionName;
}
}

/**
* Node.js stack line parser
*
* This is in @sentry/utils so it can be used from the Electron SDK in the browser for when `nodeIntegration == true`.
* This allows it to be used without referencing or importing any node specific code which causes bundlers to complain
*/
export function nodeStackLineParser(getModule?: GetModuleFn): StackLineParser {
return [90, node(getModule)];
}
14 changes: 7 additions & 7 deletions packages/utils/test/stacktrace.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nodeStackLineParser, stripSentryFramesAndReverse } from '../src/stacktrace';
import { nodeStackLineParser, stripSentryFramesAndReverse } from '../src';

describe('Stacktrace', () => {
describe('stripSentryFramesAndReverse()', () => {
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('node', () => {
const expectedOutput = {
filename: '/path/to/file.js',
module: undefined,
function: '<anonymous>',
function: '?',
lineno: 10,
colno: 5,
in_app: true,
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('node', () => {

expect(result).toEqual({
filename: '/path/to/myFile.js',
function: 'Object.<anonymous>',
function: 'Object.?',
lineno: 10,
colno: 20,
in_app: true,
Expand All @@ -269,7 +269,7 @@ describe('node', () => {
const result = node(line);
expect(result).toEqual({
filename: '/path/to/myFile.js',
function: '<anonymous>',
function: '?',
lineno: 10,
colno: 20,
in_app: true,
Expand All @@ -281,7 +281,7 @@ describe('node', () => {
const result = node(line);
expect(result).toEqual({
filename: undefined,
function: 'Object.<anonymous>',
function: 'Object.?',
lineno: undefined,
colno: undefined,
in_app: false,
Expand All @@ -294,7 +294,7 @@ describe('node', () => {

expect(result).toEqual({
filename: '/path/to/node_modules/myModule/index.js',
function: 'Object.<anonymous>',
function: 'Object.?',
lineno: 10,
colno: 20,
in_app: false,
Expand All @@ -306,7 +306,7 @@ describe('node', () => {
const result = node(line);
expect(result).toEqual({
filename: 'C:\\path\\to\\myFile.js',
function: 'Object.<anonymous>',
function: 'Object.?',
lineno: 10,
colno: 20,
in_app: true,
Expand Down