Skip to content

Commit bbe0b49

Browse files
lforstlobsterkatie
authored andcommitted
ref: Switch to magic string for logger statements
1 parent bae51df commit bbe0b49

File tree

98 files changed

+198
-416
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+198
-416
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ build/
1010
dist/
1111
coverage/
1212
scratch/
13-
*.d.ts
1413
*.js.map
1514
*.pyc
1615
*.tsbuildinfo

CONTRIBUTING.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,14 @@ Pro tip: If any of your breakpoints are in code run by multiple tests, and you r
6161

6262
## Debug Build Flags
6363

64-
Throughout the codebase, you will find debug flags like `IS_DEBUG_BUILD` guarding various code sections.
64+
Throughout the codebase, you will find debug flags like `__DEBUG_BUILD__` guarding various code sections.
6565
These flags serve two purposes:
6666

6767
1. They enable us to remove debug code for our production browser bundles.
6868
2. Enable users to tree-shake Sentry debug code for their production builds.
6969

70-
These debug flags need to be declared in each package individually and must not be imported across package boundaries, because some build tools have trouble tree-shaking imported guards.
71-
As a convention, we define debug flags in a `flags.ts` file in the root of a package's `src` folder.
72-
The `flags.ts` file will contain "magic strings" like `__SENTRY_DEBUG__` that may get replaced with actual values during our, or the user's build process.
73-
Take care when introducing new flags - they must not throw if they are not replaced.
70+
Flags will be replaced with booleans for our browser bundles, or with statements that do not throw for cjs and esm bundles.
71+
Take care when introducing new flags - they must be disarmed in our build scripts!
7472

7573
## Linting
7674

jest/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
'ts-jest': {
1313
tsconfig: '<rootDir>/tsconfig.test.json',
1414
},
15+
__DEBUG_BUILD__: true,
1516
},
1617
testPathIgnorePatterns: ['<rootDir>/build/', '<rootDir>/node_modules/'],
1718
};

packages/browser/src/client.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '@sentry/utils';
1111

1212
import { eventFromException, eventFromMessage } from './eventbuilder';
13-
import { IS_DEBUG_BUILD } from './flags';
1413
import { Breadcrumbs } from './integrations';
1514
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs';
1615
import { BrowserTransportOptions } from './transports/types';
@@ -151,24 +150,24 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
151150
const outcomes = this._clearOutcomes();
152151

153152
if (outcomes.length === 0) {
154-
IS_DEBUG_BUILD && logger.log('No outcomes to send');
153+
__DEBUG_BUILD__ && logger.log('No outcomes to send');
155154
return;
156155
}
157156

158157
if (!this._dsn) {
159-
IS_DEBUG_BUILD && logger.log('No dsn provided, will not send outcomes');
158+
__DEBUG_BUILD__ && logger.log('No dsn provided, will not send outcomes');
160159
return;
161160
}
162161

163-
IS_DEBUG_BUILD && logger.log('Sending outcomes:', outcomes);
162+
__DEBUG_BUILD__ && logger.log('Sending outcomes:', outcomes);
164163

165164
const url = getEnvelopeEndpointWithUrlEncodedAuth(this._dsn, this._options.tunnel);
166165
const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn));
167166

168167
try {
169168
sendReport(url, serializeEnvelope(envelope));
170169
} catch (e) {
171-
IS_DEBUG_BUILD && logger.error(e);
170+
__DEBUG_BUILD__ && logger.error(e);
172171
}
173172
}
174173
}

packages/browser/src/flags.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/browser/src/globals.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const __DEBUG_BUILD__: boolean;

packages/browser/src/integrations/dedupe.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Event, EventProcessor, Exception, Hub, Integration, StackFrame } from '@sentry/types';
22
import { logger } from '@sentry/utils';
33

4-
import { IS_DEBUG_BUILD } from '../flags';
5-
64
/** Deduplication filter */
75
export class Dedupe implements Integration {
86
/**
@@ -30,7 +28,7 @@ export class Dedupe implements Integration {
3028
// Juuust in case something goes wrong
3129
try {
3230
if (_shouldDropEvent(currentEvent, self._previousEvent)) {
33-
IS_DEBUG_BUILD && logger.warn('Event dropped due to being a duplicate of previously captured event.');
31+
__DEBUG_BUILD__ && logger.warn('Event dropped due to being a duplicate of previously captured event.');
3432
return null;
3533
}
3634
} catch (_oO) {

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313

1414
import { BrowserClient } from '../client';
1515
import { eventFromUnknownInput } from '../eventbuilder';
16-
import { IS_DEBUG_BUILD } from '../flags';
1716
import { shouldIgnoreOnError } from '../helpers';
1817

1918
type GlobalHandlersIntegrationsOptionKeys = 'onerror' | 'onunhandledrejection';
@@ -238,7 +237,7 @@ function _enhanceEventWithInitialFrame(event: Event, url: any, line: any, column
238237
}
239238

240239
function globalHandlerLog(type: string): void {
241-
IS_DEBUG_BUILD && logger.log(`Global Handler attached: ${type}`);
240+
__DEBUG_BUILD__ && logger.log(`Global Handler attached: ${type}`);
242241
}
243242

244243
function addMechanismAndCapture(hub: Hub, error: EventHint['originalException'], event: Event, type: string): void {

packages/browser/src/sdk.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from '@sentry/utils';
1717

1818
import { BrowserClient, BrowserClientOptions, BrowserOptions } from './client';
19-
import { IS_DEBUG_BUILD } from './flags';
2019
import { ReportDialogOptions, wrap as internalWrap } from './helpers';
2120
import { Breadcrumbs, Dedupe, GlobalHandlers, HttpContext, LinkedErrors, TryCatch } from './integrations';
2221
import { defaultStackParser } from './stack-parsers';
@@ -131,14 +130,14 @@ export function showReportDialog(options: ReportDialogOptions = {}, hub: Hub = g
131130
// doesn't work without a document (React Native)
132131
const global = getGlobalObject<Window>();
133132
if (!global.document) {
134-
IS_DEBUG_BUILD && logger.error('Global document not defined in showReportDialog call');
133+
__DEBUG_BUILD__ && logger.error('Global document not defined in showReportDialog call');
135134
return;
136135
}
137136

138137
const { client, scope } = hub.getStackTop();
139138
const dsn = options.dsn || (client && client.getDsn());
140139
if (!dsn) {
141-
IS_DEBUG_BUILD && logger.error('DSN not configured for showReportDialog call');
140+
__DEBUG_BUILD__ && logger.error('DSN not configured for showReportDialog call');
142141
return;
143142
}
144143

@@ -166,7 +165,7 @@ export function showReportDialog(options: ReportDialogOptions = {}, hub: Hub = g
166165
if (injectionPoint) {
167166
injectionPoint.appendChild(script);
168167
} else {
169-
IS_DEBUG_BUILD && logger.error('Not injecting report dialog. No injection point found in HTML');
168+
__DEBUG_BUILD__ && logger.error('Not injecting report dialog. No injection point found in HTML');
170169
}
171170
}
172171

@@ -208,7 +207,7 @@ export function flush(timeout?: number): PromiseLike<boolean> {
208207
if (client) {
209208
return client.flush(timeout);
210209
}
211-
IS_DEBUG_BUILD && logger.warn('Cannot flush events. No client defined.');
210+
__DEBUG_BUILD__ && logger.warn('Cannot flush events. No client defined.');
212211
return resolvedSyncPromise(false);
213212
}
214213

@@ -225,7 +224,7 @@ export function close(timeout?: number): PromiseLike<boolean> {
225224
if (client) {
226225
return client.close(timeout);
227226
}
228-
IS_DEBUG_BUILD && logger.warn('Cannot flush events and disable SDK. No client defined.');
227+
__DEBUG_BUILD__ && logger.warn('Cannot flush events and disable SDK. No client defined.');
229228
return resolvedSyncPromise(false);
230229
}
231230

@@ -254,7 +253,8 @@ function startSessionTracking(): void {
254253
const document = window.document;
255254

256255
if (typeof document === 'undefined') {
257-
IS_DEBUG_BUILD && logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
256+
__DEBUG_BUILD__ &&
257+
logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
258258
return;
259259
}
260260

packages/browser/src/transports/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { getGlobalObject, isNativeFetch, logger, supportsFetch } from '@sentry/utils';
22

3-
import { IS_DEBUG_BUILD } from '../flags';
4-
53
const global = getGlobalObject<Window>();
64
let cachedFetchImpl: FetchImpl;
75

@@ -71,7 +69,7 @@ export function getNativeFetchImplementation(): FetchImpl {
7169
}
7270
document.head.removeChild(sandbox);
7371
} catch (e) {
74-
IS_DEBUG_BUILD &&
72+
__DEBUG_BUILD__ &&
7573
logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e);
7674
}
7775
}

packages/browser/test/globals.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const __DEBUG_BUILD__: boolean;

packages/core/src/baseclient.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939

4040
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
4141
import { createEventEnvelope, createSessionEnvelope } from './envelope';
42-
import { IS_DEBUG_BUILD } from './flags';
4342
import { IntegrationIndex, setupIntegrations } from './integration';
4443

4544
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
@@ -112,7 +111,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
112111
url,
113112
});
114113
} else {
115-
IS_DEBUG_BUILD && logger.warn('No DSN provided, client will not do anything.');
114+
__DEBUG_BUILD__ && logger.warn('No DSN provided, client will not do anything.');
116115
}
117116
}
118117

@@ -123,7 +122,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
123122
public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined {
124123
// ensure we haven't captured this very object before
125124
if (checkOrSetAlreadyCaught(exception)) {
126-
IS_DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR);
125+
__DEBUG_BUILD__ && logger.log(ALREADY_SEEN_ERROR);
127126
return;
128127
}
129128

@@ -173,7 +172,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
173172
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
174173
// ensure we haven't captured this very object before
175174
if (hint && hint.originalException && checkOrSetAlreadyCaught(hint.originalException)) {
176-
IS_DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR);
175+
__DEBUG_BUILD__ && logger.log(ALREADY_SEEN_ERROR);
177176
return;
178177
}
179178

@@ -193,12 +192,12 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
193192
*/
194193
public captureSession(session: Session): void {
195194
if (!this._isEnabled()) {
196-
IS_DEBUG_BUILD && logger.warn('SDK not enabled, will not capture session.');
195+
__DEBUG_BUILD__ && logger.warn('SDK not enabled, will not capture session.');
197196
return;
198197
}
199198

200199
if (!(typeof session.release === 'string')) {
201-
IS_DEBUG_BUILD && logger.warn('Discarded session because of missing or non-string release');
200+
__DEBUG_BUILD__ && logger.warn('Discarded session because of missing or non-string release');
202201
} else {
203202
this.sendSession(session);
204203
// After sending, we set init false to indicate it's not the first occurrence
@@ -277,7 +276,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
277276
try {
278277
return (this._integrations[integration.id] as T) || null;
279278
} catch (_oO) {
280-
IS_DEBUG_BUILD && logger.warn(`Cannot retrieve integration ${integration.id} from the current Client`);
279+
__DEBUG_BUILD__ && logger.warn(`Cannot retrieve integration ${integration.id} from the current Client`);
281280
return null;
282281
}
283282
}
@@ -319,7 +318,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
319318
// would be `Partial<Record<SentryRequestType, Partial<Record<Outcome, number>>>>`
320319
// With typescript 4.1 we could even use template literal types
321320
const key = `${reason}:${category}`;
322-
IS_DEBUG_BUILD && logger.log(`Adding outcome: "${key}"`);
321+
__DEBUG_BUILD__ && logger.log(`Adding outcome: "${key}"`);
323322

324323
// The following works because undefined + 1 === NaN and NaN is falsy
325324
this._outcomes[key] = this._outcomes[key] + 1 || 1;
@@ -572,7 +571,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
572571
return finalEvent.event_id;
573572
},
574573
reason => {
575-
IS_DEBUG_BUILD && logger.warn(reason);
574+
__DEBUG_BUILD__ && logger.warn(reason);
576575
return undefined;
577576
},
578577
);
@@ -680,10 +679,10 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
680679
protected _sendEnvelope(envelope: Envelope): void {
681680
if (this._transport && this._dsn) {
682681
this._transport.send(envelope).then(null, reason => {
683-
IS_DEBUG_BUILD && logger.error('Error while sending event:', reason);
682+
__DEBUG_BUILD__ && logger.error('Error while sending event:', reason);
684683
});
685684
} else {
686-
IS_DEBUG_BUILD && logger.error('Transport disabled');
685+
__DEBUG_BUILD__ && logger.error('Transport disabled');
687686
}
688687
}
689688

packages/core/src/flags.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/core/src/globals.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const __DEBUG_BUILD__: boolean;

packages/core/src/integration.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
22
import { Integration, Options } from '@sentry/types';
33
import { logger } from '@sentry/utils';
44

5-
import { IS_DEBUG_BUILD } from './flags';
6-
75
export const installedIntegrations: string[] = [];
86

97
/** Map of integrations assigned to a client */
@@ -69,7 +67,7 @@ export function setupIntegrations(integrations: Integration[]): IntegrationIndex
6967
if (installedIntegrations.indexOf(integration.name) === -1) {
7068
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
7169
installedIntegrations.push(integration.name);
72-
IS_DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);
70+
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
7371
}
7472
});
7573

packages/core/src/integrations/inboundfilters.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Event, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';
22
import { getEventDescription, isMatchingPattern, logger } from '@sentry/utils';
33

4-
import { IS_DEBUG_BUILD } from '../flags';
5-
64
// "Script error." is hard coded into browsers for errors that it can't read.
75
// this is the result of a script being pulled in from an external domain and CORS.
86
const DEFAULT_IGNORE_ERRORS = [/^Script error\.?$/, /^Javascript error: Script error\.? on line 0$/];
@@ -72,19 +70,19 @@ export function _mergeOptions(
7270
/** JSDoc */
7371
export function _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean {
7472
if (options.ignoreInternal && _isSentryError(event)) {
75-
IS_DEBUG_BUILD &&
73+
__DEBUG_BUILD__ &&
7674
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
7775
return true;
7876
}
7977
if (_isIgnoredError(event, options.ignoreErrors)) {
80-
IS_DEBUG_BUILD &&
78+
__DEBUG_BUILD__ &&
8179
logger.warn(
8280
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
8381
);
8482
return true;
8583
}
8684
if (_isDeniedUrl(event, options.denyUrls)) {
87-
IS_DEBUG_BUILD &&
85+
__DEBUG_BUILD__ &&
8886
logger.warn(
8987
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
9088
event,
@@ -93,7 +91,7 @@ export function _shouldDropEvent(event: Event, options: Partial<InboundFiltersOp
9391
return true;
9492
}
9593
if (!_isAllowedUrl(event, options.allowUrls)) {
96-
IS_DEBUG_BUILD &&
94+
__DEBUG_BUILD__ &&
9795
logger.warn(
9896
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
9997
event,
@@ -141,7 +139,7 @@ function _getPossibleEventMessages(event: Event): string[] {
141139
const { type = '', value = '' } = (event.exception.values && event.exception.values[0]) || {};
142140
return [`${value}`, `${type}: ${value}`];
143141
} catch (oO) {
144-
IS_DEBUG_BUILD && logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
142+
__DEBUG_BUILD__ && logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
145143
return [];
146144
}
147145
}
@@ -182,7 +180,7 @@ function _getEventFilterUrl(event: Event): string | null {
182180
}
183181
return frames ? _getLastValidUrl(frames) : null;
184182
} catch (oO) {
185-
IS_DEBUG_BUILD && logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
183+
__DEBUG_BUILD__ && logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
186184
return null;
187185
}
188186
}

0 commit comments

Comments
 (0)