Skip to content

fix: Always use ? for anonymous function name #5664

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

Closed
Closed
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
Expand Up @@ -9,6 +9,7 @@ import {
isPrimitive,
isString,
logger,
UNKNOWN_FUNCTION,
} from '@sentry/utils';

import { BrowserClient } from '../client';
Expand Down Expand Up @@ -227,7 +228,7 @@ function _enhanceEventWithInitialFrame(event: Event, url: any, line: any, column
ev0sf.push({
colno,
filename,
function: '?',
function: UNKNOWN_FUNCTION,
in_app: true,
lineno,
});
Expand Down
5 changes: 1 addition & 4 deletions packages/browser/src/stack-parsers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { StackFrame, StackLineParser, StackLineParserFn } from '@sentry/types';
import { createStackParser } from '@sentry/utils';

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

const OPERA10_PRIORITY = 10;
const OPERA11_PRIORITY = 20;
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/test/integration/suites/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ describe('wrapped built-ins', function () {
});

it(
optional('should fallback to <anonymous> fn name in mechanism data if one is unavailable', IS_LOADER),
optional('should fallback to "?" fn name in mechanism data if one is unavailable', IS_LOADER),
function () {
return runInSandbox(sandbox, function () {
var div = document.createElement('div');
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('wrapped built-ins', function () {
handled: true,
data: {
function: 'addEventListener',
handler: '<anonymous>',
handler: '?',
},
});
}
Expand Down
5 changes: 4 additions & 1 deletion packages/integrations/src/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';
import { UNKNOWN_FUNCTION } from '@sentry/utils';

/** Add node transaction to the event */
export class Transaction implements Integration {
Expand Down Expand Up @@ -52,6 +53,8 @@ export class Transaction implements Integration {

/** JSDoc */
private _getTransaction(frame: StackFrame): string {
return frame.module || frame.function ? `${frame.module || '?'}/${frame.function || '?'}` : '<unknown>';
return frame.module || frame.function
? `${frame.module || UNKNOWN_FUNCTION}/${frame.function || UNKNOWN_FUNCTION}`
: '<unknown>';
}
}
6 changes: 3 additions & 3 deletions packages/node/test/stacktrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,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 @@ -212,7 +212,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 @@ -289,7 +289,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
15 changes: 7 additions & 8 deletions packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StackFrame, StackLineParser, StackLineParserFn, StackParser } from '@sentry/types';

const STACKTRACE_LIMIT = 50;
export const UNKNOWN_FUNCTION = '?';

/**
* Creates a stack parser with the supplied line parsers
Expand Down Expand Up @@ -76,26 +77,24 @@ export function stripSentryFramesAndReverse(stack: StackFrame[]): StackFrame[] {
.map(frame => ({
...frame,
filename: frame.filename || localStack[0].filename,
function: frame.function || '?',
function: frame.function || UNKNOWN_FUNCTION,
}))
.reverse();
}

const defaultFunctionName = '<anonymous>';

/**
* Safely extract function name from itself
*/
export function getFunctionName(fn: unknown): string {
try {
if (!fn || typeof fn !== 'function') {
return defaultFunctionName;
return UNKNOWN_FUNCTION;
}
return fn.name || defaultFunctionName;
return fn.name || UNKNOWN_FUNCTION;
} catch (e) {
// Just accessing custom props in some Selenium environments
// can cause a "Permission denied" exception (see raven-js#495).
return defaultFunctionName;
return UNKNOWN_FUNCTION;
}
}

Expand Down Expand Up @@ -151,13 +150,13 @@ function node(getModule?: GetModuleFn): StackLineParserFn {
methodName = method;
}

if (method === '<anonymous>') {
if (method === UNKNOWN_FUNCTION) {
methodName = undefined;
functionName = undefined;
}

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

Expand Down
4 changes: 2 additions & 2 deletions packages/utils/test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('normalize()', () => {
subject.toJSON = () => {
throw new Error("I'm faulty!");
};
expect(normalize(subject)).toEqual({ a: 1, foo: 'bar', toJSON: '[Function: <anonymous>]' });
expect(normalize(subject)).toEqual({ a: 1, foo: 'bar', toJSON: '[Function: ?]' });
});

test('should return an object without circular references when toJSON returns an object with circular references', () => {
Expand Down Expand Up @@ -327,7 +327,7 @@ describe('normalize()', () => {
normalize(() => {
/* no-empty */
}),
).toEqual('[Function: <anonymous>]');
).toEqual('[Function: ?]');
const foo = () => {
/* no-empty */
};
Expand Down