Skip to content

Commit f16ea8f

Browse files
committed
Merge branch 'master' of https://github.com/getsentry/sentry-javascript into onur/public-api-tests-capture-message
2 parents e502382 + 4cd71c5 commit f16ea8f

File tree

83 files changed

+979
-324
lines changed

Some content is hidden

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

83 files changed

+979
-324
lines changed

packages/browser/src/helpers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
addNonEnumerableProperty,
77
getGlobalObject,
88
getOriginalFunction,
9+
isDebugBuild,
910
logger,
1011
markFunctionWrapped,
1112
} from '@sentry/utils';
@@ -191,12 +192,16 @@ export function injectReportDialog(options: ReportDialogOptions = {}): void {
191192
}
192193

193194
if (!options.eventId) {
194-
logger.error(`Missing eventId option in showReportDialog call`);
195+
if (isDebugBuild()) {
196+
logger.error(`Missing eventId option in showReportDialog call`);
197+
}
195198
return;
196199
}
197200

198201
if (!options.dsn) {
199-
logger.error(`Missing dsn option in showReportDialog call`);
202+
if (isDebugBuild()) {
203+
logger.error(`Missing dsn option in showReportDialog call`);
204+
}
200205
return;
201206
}
202207

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
addExceptionMechanism,
66
addInstrumentationHandler,
77
getLocationHref,
8+
isDebugBuild,
89
isErrorEvent,
910
isPrimitive,
1011
isString,
@@ -239,7 +240,9 @@ function _enhanceEventWithInitialFrame(event: Event, url: any, line: any, column
239240
}
240241

241242
function globalHandlerLog(type: string): void {
242-
logger.log(`Global Handler attached: ${type}`);
243+
if (isDebugBuild()) {
244+
logger.log(`Global Handler attached: ${type}`);
245+
}
243246
}
244247

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

packages/browser/src/sdk.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
2-
import { addInstrumentationHandler, getGlobalObject, logger, resolvedSyncPromise } from '@sentry/utils';
2+
import { Hub } from '@sentry/types';
3+
import { addInstrumentationHandler, getGlobalObject, isDebugBuild, logger, resolvedSyncPromise } from '@sentry/utils';
34

45
import { BrowserOptions } from './backend';
56
import { BrowserClient } from './client';
@@ -161,7 +162,9 @@ export function flush(timeout?: number): PromiseLike<boolean> {
161162
if (client) {
162163
return client.flush(timeout);
163164
}
164-
logger.warn('Cannot flush events. No client defined.');
165+
if (isDebugBuild()) {
166+
logger.warn('Cannot flush events. No client defined.');
167+
}
165168
return resolvedSyncPromise(false);
166169
}
167170

@@ -178,7 +181,9 @@ export function close(timeout?: number): PromiseLike<boolean> {
178181
if (client) {
179182
return client.close(timeout);
180183
}
181-
logger.warn('Cannot flush events and disable SDK. No client defined.');
184+
if (isDebugBuild()) {
185+
logger.warn('Cannot flush events and disable SDK. No client defined.');
186+
}
182187
return resolvedSyncPromise(false);
183188
}
184189

@@ -194,6 +199,11 @@ export function wrap(fn: (...args: any) => any): any {
194199
return internalWrap(fn)();
195200
}
196201

202+
function startSessionOnHub(hub: Hub): void {
203+
hub.startSession({ ignoreDuration: true });
204+
hub.captureSession();
205+
}
206+
197207
/**
198208
* Enable automatic Session Tracking for the initial page load.
199209
*/
@@ -202,7 +212,9 @@ function startSessionTracking(): void {
202212
const document = window.document;
203213

204214
if (typeof document === 'undefined') {
205-
logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
215+
if (isDebugBuild()) {
216+
logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
217+
}
206218
return;
207219
}
208220

@@ -214,24 +226,21 @@ function startSessionTracking(): void {
214226
// https://github.com/getsentry/sentry-javascript/issues/3207 and
215227
// https://github.com/getsentry/sentry-javascript/issues/3234 and
216228
// https://github.com/getsentry/sentry-javascript/issues/3278.
217-
if (typeof hub.startSession !== 'function' || typeof hub.captureSession !== 'function') {
229+
if (!hub.captureSession) {
218230
return;
219231
}
220232

221233
// The session duration for browser sessions does not track a meaningful
222234
// concept that can be used as a metric.
223235
// Automatically captured sessions are akin to page views, and thus we
224236
// discard their duration.
225-
hub.startSession({ ignoreDuration: true });
226-
hub.captureSession();
237+
startSessionOnHub(hub);
227238

228239
// We want to create a session for every navigation as well
229240
addInstrumentationHandler('history', ({ from, to }) => {
230241
// Don't create an additional session for the initial route or if the location did not change
231-
if (from === undefined || from === to) {
232-
return;
242+
if (!(from === undefined || from === to)) {
243+
startSessionOnHub(getCurrentHub());
233244
}
234-
hub.startSession({ ignoreDuration: true });
235-
hub.captureSession();
236245
});
237246
}

packages/browser/src/transports/base.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import {
22
APIDetails,
3+
eventToSentryRequest,
34
getEnvelopeEndpointWithUrlEncodedAuth,
45
getStoreEndpointWithUrlEncodedAuth,
56
initAPIDetails,
7+
sessionToSentryRequest,
68
} from '@sentry/core';
79
import {
810
Event,
911
Outcome,
1012
Response as SentryResponse,
13+
SentryRequest,
1114
SentryRequestType,
15+
Session,
1216
Transport,
1317
TransportOptions,
1418
} from '@sentry/types';
1519
import {
1620
dateTimestampInSeconds,
21+
dsnToString,
1722
eventStatusFromHttpCode,
1823
getGlobalObject,
24+
isDebugBuild,
1925
logger,
2026
makePromiseBuffer,
2127
parseRetryAfterHeader,
2228
PromiseBuffer,
23-
SentryError,
2429
} from '@sentry/utils';
2530

2631
import { sendReport } from './utils';
@@ -67,8 +72,15 @@ export abstract class BaseTransport implements Transport {
6772
/**
6873
* @inheritDoc
6974
*/
70-
public sendEvent(_: Event): PromiseLike<SentryResponse> {
71-
throw new SentryError('Transport Class has to implement `sendEvent` method');
75+
public sendEvent(event: Event): PromiseLike<SentryResponse> {
76+
return this._sendRequest(eventToSentryRequest(event, this._api), event);
77+
}
78+
79+
/**
80+
* @inheritDoc
81+
*/
82+
public sendSession(session: Session): PromiseLike<SentryResponse> {
83+
return this._sendRequest(sessionToSentryRequest(session, this._api), session);
7284
}
7385

7486
/**
@@ -116,7 +128,7 @@ export abstract class BaseTransport implements Transport {
116128

117129
const url = getEnvelopeEndpointWithUrlEncodedAuth(this._api.dsn, this._api.tunnel);
118130
// Envelope header is required to be at least an empty object
119-
const envelopeHeader = JSON.stringify({ ...(this._api.tunnel && { dsn: this._api.dsn.toString() }) });
131+
const envelopeHeader = JSON.stringify({ ...(this._api.tunnel && { dsn: dsnToString(this._api.dsn) }) });
120132
const itemHeaders = JSON.stringify({
121133
type: 'client_report',
122134
});
@@ -162,8 +174,9 @@ export abstract class BaseTransport implements Transport {
162174
* https://developer.mozilla.org/en-US/docs/Web/API/Headers/get
163175
*/
164176
const limited = this._handleRateLimit(headers);
165-
if (limited)
177+
if (limited && isDebugBuild()) {
166178
logger.warn(`Too many ${requestType} requests, backing off until: ${this._disabledUntil(requestType)}`);
179+
}
167180

168181
if (status === 'success') {
169182
resolve({ status });
@@ -222,4 +235,9 @@ export abstract class BaseTransport implements Transport {
222235
}
223236
return false;
224237
}
238+
239+
protected abstract _sendRequest(
240+
sentryRequest: SentryRequest,
241+
originalPayload: Event | Session,
242+
): PromiseLike<SentryResponse>;
225243
}

packages/browser/src/transports/fetch.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { eventToSentryRequest, sessionToSentryRequest } from '@sentry/core';
21
import { Event, Response, SentryRequest, Session, TransportOptions } from '@sentry/types';
32
import { SentryError, supportsReferrerPolicy, SyncPromise } from '@sentry/utils';
43

@@ -17,25 +16,11 @@ export class FetchTransport extends BaseTransport {
1716
this._fetch = fetchImpl;
1817
}
1918

20-
/**
21-
* @inheritDoc
22-
*/
23-
public sendEvent(event: Event): PromiseLike<Response> {
24-
return this._sendRequest(eventToSentryRequest(event, this._api), event);
25-
}
26-
27-
/**
28-
* @inheritDoc
29-
*/
30-
public sendSession(session: Session): PromiseLike<Response> {
31-
return this._sendRequest(sessionToSentryRequest(session, this._api), session);
32-
}
33-
3419
/**
3520
* @param sentryRequest Prepared SentryRequest to be delivered
3621
* @param originalPayload Original payload used to create SentryRequest
3722
*/
38-
private _sendRequest(sentryRequest: SentryRequest, originalPayload: Event | Session): PromiseLike<Response> {
23+
protected _sendRequest(sentryRequest: SentryRequest, originalPayload: Event | Session): PromiseLike<Response> {
3924
if (this._isRateLimited(sentryRequest.type)) {
4025
this.recordLostEvent('ratelimit_backoff', sentryRequest.type);
4126

packages/browser/src/transports/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { forget, getGlobalObject, isNativeFetch, logger, supportsFetch } from '@sentry/utils';
1+
import { forget, getGlobalObject, isDebugBuild, isNativeFetch, logger, supportsFetch } from '@sentry/utils';
22

33
const global = getGlobalObject<Window>();
44
let cachedFetchImpl: FetchImpl;
@@ -69,7 +69,9 @@ export function getNativeFetchImplementation(): FetchImpl {
6969
}
7070
document.head.removeChild(sandbox);
7171
} catch (e) {
72-
logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e);
72+
if (isDebugBuild()) {
73+
logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e);
74+
}
7375
}
7476
}
7577

packages/browser/src/transports/xhr.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
1-
import { eventToSentryRequest, sessionToSentryRequest } from '@sentry/core';
21
import { Event, Response, SentryRequest, Session } from '@sentry/types';
32
import { SentryError, SyncPromise } from '@sentry/utils';
43

54
import { BaseTransport } from './base';
65

76
/** `XHR` based transport */
87
export class XHRTransport extends BaseTransport {
9-
/**
10-
* @inheritDoc
11-
*/
12-
public sendEvent(event: Event): PromiseLike<Response> {
13-
return this._sendRequest(eventToSentryRequest(event, this._api), event);
14-
}
15-
16-
/**
17-
* @inheritDoc
18-
*/
19-
public sendSession(session: Session): PromiseLike<Response> {
20-
return this._sendRequest(sessionToSentryRequest(session, this._api), session);
21-
}
22-
238
/**
249
* @param sentryRequest Prepared SentryRequest to be delivered
2510
* @param originalPayload Original payload used to create SentryRequest
2611
*/
27-
private _sendRequest(sentryRequest: SentryRequest, originalPayload: Event | Session): PromiseLike<Response> {
12+
protected _sendRequest(sentryRequest: SentryRequest, originalPayload: Event | Session): PromiseLike<Response> {
2813
if (this._isRateLimited(sentryRequest.type)) {
2914
this.recordLostEvent('ratelimit_backoff', sentryRequest.type);
3015

packages/browser/test/unit/integrations/helpers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { WrappedFunction } from '@sentry/types';
2-
import { SinonSpy, spy } from 'sinon';
2+
import { spy } from 'sinon';
33

44
import { wrap } from '../../../src/helpers';
55

packages/browser/test/unit/transports/base.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,13 @@ describe('BaseTransport', () => {
112112
});
113113

114114
it('doesnt provide sendEvent() implementation', () => {
115+
expect.assertions(1);
115116
const transport = new SimpleTransport({ dsn: testDsn });
116117

117118
try {
118119
void transport.sendEvent({});
119120
} catch (e) {
120-
expect(e.message).toBe('Transport Class has to implement `sendEvent` method');
121+
expect(e).toBeDefined();
121122
}
122123
});
123124

0 commit comments

Comments
 (0)