From 6c22fa528594ca76ca1e352bf73d275b988b3c88 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 31 Aug 2022 17:55:35 +0100 Subject: [PATCH 1/2] Always use `?` for anonymous function name --- .../browser/src/integrations/globalhandlers.ts | 3 ++- packages/browser/src/stack-parsers.ts | 5 +---- packages/integrations/src/transaction.ts | 5 ++++- packages/node/test/stacktrace.test.ts | 6 +++--- packages/utils/src/stacktrace.ts | 15 +++++++-------- packages/utils/test/normalize.test.ts | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/browser/src/integrations/globalhandlers.ts b/packages/browser/src/integrations/globalhandlers.ts index 79d5d3d6a444..383d9257f51b 100644 --- a/packages/browser/src/integrations/globalhandlers.ts +++ b/packages/browser/src/integrations/globalhandlers.ts @@ -9,6 +9,7 @@ import { isPrimitive, isString, logger, + UNKNOWN_FUNCTION, } from '@sentry/utils'; import { BrowserClient } from '../client'; @@ -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, }); diff --git a/packages/browser/src/stack-parsers.ts b/packages/browser/src/stack-parsers.ts index eeb08c882d8c..bb327befcfce 100644 --- a/packages/browser/src/stack-parsers.ts +++ b/packages/browser/src/stack-parsers.ts @@ -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; diff --git a/packages/integrations/src/transaction.ts b/packages/integrations/src/transaction.ts index e6d0ac24f770..dba8d632c556 100644 --- a/packages/integrations/src/transaction.ts +++ b/packages/integrations/src/transaction.ts @@ -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 { @@ -52,6 +53,8 @@ export class Transaction implements Integration { /** JSDoc */ private _getTransaction(frame: StackFrame): string { - return frame.module || frame.function ? `${frame.module || '?'}/${frame.function || '?'}` : ''; + return frame.module || frame.function + ? `${frame.module || UNKNOWN_FUNCTION}/${frame.function || UNKNOWN_FUNCTION}` + : ''; } } diff --git a/packages/node/test/stacktrace.test.ts b/packages/node/test/stacktrace.test.ts index f5a1b453609f..c6a2bb0adcb8 100644 --- a/packages/node/test/stacktrace.test.ts +++ b/packages/node/test/stacktrace.test.ts @@ -192,7 +192,7 @@ describe('Stack parsing', () => { { filename: '/Users/felix/code/node-fast-or-slow/lib/test_case.js', module: 'test_case', - function: '', + function: '?', lineno: 80, colno: 10, in_app: true, @@ -212,7 +212,7 @@ describe('Stack parsing', () => { { filename: '/Users/felix/code/node-fast-or-slow/lib/test_case.js', module: 'test_case', - function: '', + function: '?', lineno: 80, colno: 10, in_app: true, @@ -289,7 +289,7 @@ describe('Stack parsing', () => { { filename: '/code/node_modules/kafkajs/src/consumer/runner.js', module: 'kafkajs.src.consumer:runner', - function: '', + function: '?', lineno: 376, colno: 15, in_app: false, diff --git a/packages/utils/src/stacktrace.ts b/packages/utils/src/stacktrace.ts index 87e27aa750cd..63cf1f275d29 100644 --- a/packages/utils/src/stacktrace.ts +++ b/packages/utils/src/stacktrace.ts @@ -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 @@ -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 = ''; - /** * 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; } } @@ -151,13 +150,13 @@ function node(getModule?: GetModuleFn): StackLineParserFn { methodName = method; } - if (method === '') { + if (method === UNKNOWN_FUNCTION) { methodName = undefined; functionName = undefined; } if (functionName === undefined) { - methodName = methodName || ''; + methodName = methodName || UNKNOWN_FUNCTION; functionName = typeName ? `${typeName}.${methodName}` : methodName; } diff --git a/packages/utils/test/normalize.test.ts b/packages/utils/test/normalize.test.ts index 9f6b60f5f392..aa65a4ee8a61 100644 --- a/packages/utils/test/normalize.test.ts +++ b/packages/utils/test/normalize.test.ts @@ -291,7 +291,7 @@ describe('normalize()', () => { subject.toJSON = () => { throw new Error("I'm faulty!"); }; - expect(normalize(subject)).toEqual({ a: 1, foo: 'bar', toJSON: '[Function: ]' }); + 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', () => { @@ -327,7 +327,7 @@ describe('normalize()', () => { normalize(() => { /* no-empty */ }), - ).toEqual('[Function: ]'); + ).toEqual('[Function: ?]'); const foo = () => { /* no-empty */ }; From e595ac92ba306ab88209d37a71511131e1b79e89 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 31 Aug 2022 18:53:30 +0100 Subject: [PATCH 2/2] Fix old browser integration tests --- packages/browser/test/integration/suites/builtins.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser/test/integration/suites/builtins.js b/packages/browser/test/integration/suites/builtins.js index 8fa30d120e90..04839543af7a 100644 --- a/packages/browser/test/integration/suites/builtins.js +++ b/packages/browser/test/integration/suites/builtins.js @@ -287,7 +287,7 @@ describe('wrapped built-ins', function () { }); it( - optional('should fallback to 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'); @@ -316,7 +316,7 @@ describe('wrapped built-ins', function () { handled: true, data: { function: 'addEventListener', - handler: '', + handler: '?', }, }); }