Skip to content

Commit d68aa40

Browse files
committed
port BaseBackend to BaseClient
1 parent 381183e commit d68aa40

File tree

1 file changed

+103
-9
lines changed

1 file changed

+103
-9
lines changed

packages/core/src/baseclient.ts

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ import {
2828
uuid4,
2929
} from '@sentry/utils';
3030

31+
import { initAPIDetails } from './api';
3132
import { Backend, BackendClass } from './basebackend';
3233
import { IS_DEBUG_BUILD } from './flags';
3334
import { IntegrationIndex, setupIntegrations } from './integration';
35+
import { createEventEnvelope, createSessionEnvelope } from './request';
36+
import { NewTransport } from './transports/base';
37+
import { NoopTransport } from './transports/noop';
3438

3539
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
3640

3741
/**
3842
* Base implementation for all JavaScript SDK clients.
3943
*
44+
* TODO: refactor doc w.r.t. Backend
45+
*
4046
* Call the constructor with the corresponding backend constructor and options
4147
* specific to the client subclass. To access these options later, use
4248
* {@link Client.getOptions}. Also, the Backend instance is available via
@@ -71,6 +77,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
7177
* The backend used to physically interact in the environment. Usually, this
7278
* will correspond to the client. When composing SDKs, however, the Backend
7379
* from the root SDK will be used.
80+
* TODO: DELETE
7481
*/
7582
protected readonly _backend: B;
7683

@@ -86,19 +93,30 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
8693
/** Number of calls being processed */
8794
protected _numProcessing: number = 0;
8895

96+
/** Cached transport used internally. */
97+
protected _transport: Transport;
98+
99+
/** New v7 Transport that is initialized alongside the old one */
100+
protected _newTransport?: NewTransport;
101+
89102
/**
90103
* Initializes this client instance.
91104
*
92105
* @param backendClass A constructor function to create the backend.
93106
* @param options Options for the client.
94107
*/
95108
protected constructor(backendClass: BackendClass<B, O>, options: O) {
109+
// TODO: Delete
96110
this._backend = new backendClass(options);
97111
this._options = options;
98112

99113
if (options.dsn) {
100114
this._dsn = makeDsn(options.dsn);
115+
} else {
116+
IS_DEBUG_BUILD && logger.warn('No DSN provided, client will not do anything.');
101117
}
118+
119+
this._transport = this._setupTransport();
102120
}
103121

104122
/**
@@ -115,8 +133,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
115133
let eventId: string | undefined = hint && hint.event_id;
116134

117135
this._process(
118-
this._getBackend()
119-
.eventFromException(exception, hint)
136+
this.eventFromException(exception, hint)
120137
.then(event => this._captureEvent(event, hint, scope))
121138
.then(result => {
122139
eventId = result;
@@ -132,9 +149,10 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
132149
public captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined {
133150
let eventId: string | undefined = hint && hint.event_id;
134151

152+
// TODO: refactor
135153
const promisedEvent = isPrimitive(message)
136-
? this._getBackend().eventFromMessage(String(message), level, hint)
137-
: this._getBackend().eventFromException(message, hint);
154+
? this.eventFromMessage(String(message), level, hint)
155+
: this.eventFromException(message, hint);
138156

139157
this._process(
140158
promisedEvent
@@ -204,7 +222,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
204222
* @inheritDoc
205223
*/
206224
public getTransport(): Transport {
207-
return this._getBackend().getTransport();
225+
return this._transport;
208226
}
209227

210228
/**
@@ -249,6 +267,72 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
249267
}
250268
}
251269

270+
/**
271+
* @inheritDoc
272+
*/
273+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
274+
public eventFromException(_exception: any, _hint?: EventHint): PromiseLike<Event> {
275+
throw new SentryError('Client has to implement `eventFromException` method');
276+
}
277+
278+
/**
279+
* @inheritDoc
280+
*/
281+
public eventFromMessage(_message: string, _level?: Severity, _hint?: EventHint): PromiseLike<Event> {
282+
throw new SentryError('Client has to implement `eventFromMessage` method');
283+
}
284+
285+
/**
286+
* @inheritDoc
287+
*/
288+
public sendEvent(event: Event): void {
289+
// TODO(v7): Remove the if-else
290+
if (
291+
this._newTransport &&
292+
this._options.dsn &&
293+
this._options._experiments &&
294+
this._options._experiments.newTransport
295+
) {
296+
const api = initAPIDetails(this._options.dsn, this._options._metadata, this._options.tunnel);
297+
const env = createEventEnvelope(event, api);
298+
void this._newTransport.send(env).then(null, reason => {
299+
IS_DEBUG_BUILD && logger.error('Error while sending event:', reason);
300+
});
301+
} else {
302+
void this._transport.sendEvent(event).then(null, reason => {
303+
IS_DEBUG_BUILD && logger.error('Error while sending event:', reason);
304+
});
305+
}
306+
}
307+
308+
/**
309+
* @inheritDoc
310+
*/
311+
public sendSession(session: Session): void {
312+
if (!this._transport.sendSession) {
313+
IS_DEBUG_BUILD && logger.warn("Dropping session because custom transport doesn't implement sendSession");
314+
return;
315+
}
316+
317+
// TODO(v7): Remove the if-else
318+
if (
319+
this._newTransport &&
320+
this._options.dsn &&
321+
this._options._experiments &&
322+
this._options._experiments.newTransport
323+
) {
324+
const api = initAPIDetails(this._options.dsn, this._options._metadata, this._options.tunnel);
325+
const [env] = createSessionEnvelope(session, api);
326+
void this._newTransport.send(env).then(null, reason => {
327+
IS_DEBUG_BUILD && logger.error('Error while sending session:', reason);
328+
});
329+
} else {
330+
void this._transport.sendSession(session).then(null, reason => {
331+
IS_DEBUG_BUILD && logger.error('Error while sending session:', reason);
332+
});
333+
}
334+
}
335+
252336
/** Updates existing session based on the provided event */
253337
protected _updateSessionFromEvent(session: Session, event: Event): void {
254338
let crashed = false;
@@ -284,7 +368,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
284368

285369
/** Deliver captured session to Sentry */
286370
protected _sendSession(session: Session): void {
287-
this._getBackend().sendSession(session);
371+
this.sendSession(session);
288372
}
289373

290374
/**
@@ -317,7 +401,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
317401
});
318402
}
319403

320-
/** Returns the current backend. */
404+
/** Returns the current backend.
405+
* TODO: DELETE
406+
*/
321407
protected _getBackend(): B {
322408
return this._backend;
323409
}
@@ -490,8 +576,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
490576
* Tells the backend to send this event
491577
* @param event The Sentry event to send
492578
*/
579+
// TODO: refactor: get rid of method?
493580
protected _sendEvent(event: Event): void {
494-
this._getBackend().sendEvent(event);
581+
this.sendEvent(event);
495582
}
496583

497584
/**
@@ -582,7 +669,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
582669
this._updateSessionFromEvent(session, processedEvent);
583670
}
584671

585-
this._sendEvent(processedEvent);
672+
this.sendEvent(processedEvent);
586673
return processedEvent;
587674
})
588675
.then(null, reason => {
@@ -618,6 +705,13 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
618705
},
619706
);
620707
}
708+
709+
/**
710+
* Sets up the transport so it can be used later to send requests.
711+
*/
712+
protected _setupTransport(): Transport {
713+
return new NoopTransport();
714+
}
621715
}
622716

623717
/**

0 commit comments

Comments
 (0)