Skip to content

Commit 23d87ed

Browse files
authored
[NEXT] Frontend -> Client (#1323)
* fix: This can be null under some circumstances * ref: Rename Frontend to Client * ref: ClientBase -> BaseClient * ref: Remove client and pass options instead * ref: Add correct type checks for raven-js send
1 parent 6744eaf commit 23d87ed

21 files changed

+225
-229
lines changed

packages/browser/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ If you don't want to use a global static instance of Sentry, you can create one
6767
yourself:
6868

6969
```javascript
70-
import { BrowserFrontend } from '@sentry/browser';
70+
import { BrowserClient } from '@sentry/browser';
7171

72-
const client = new BrowserFrontend({
72+
const client = new BrowserClient({
7373
dsn: '__DSN__',
7474
// ...
7575
});

packages/browser/src/backend.ts

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Backend, Frontend, Options, SentryError } from '@sentry/core';
1+
import { Backend, Options, SentryError } from '@sentry/core';
22
import {
33
addBreadcrumb,
44
captureEvent,
@@ -11,12 +11,13 @@ import { Raven, SendMethod } from './raven';
1111
const sendRavenEvent = Raven._sendProcessedPayload.bind(Raven) as SendMethod;
1212

1313
/** Normalizes the event so it is consistent with our domain interface. */
14-
function normalizeRavenEvent(event: SentryEvent): SentryEvent {
15-
const ex = (event.exception || {}) as { values?: SentryException[] };
16-
if (ex && ex.values) {
14+
function normalizeRavenEvent(event?: SentryEvent): SentryEvent | undefined {
15+
const ex = ((event && event.exception) || {}) as {
16+
values?: SentryException[];
17+
};
18+
if (event && ex && ex.values) {
1719
event.exception = ex.values;
1820
}
19-
2021
return event;
2122
}
2223

@@ -32,7 +33,7 @@ function prepareEventForRaven(event: SentryEvent): SentryEvent {
3233

3334
/**
3435
* Configuration options for the Sentry Browser SDK.
35-
* @see BrowserFrontend for more information.
36+
* @see BrowserClient for more information.
3637
*/
3738
export interface BrowserOptions extends Options {
3839
/**
@@ -65,43 +66,41 @@ export interface BrowserOptions extends Options {
6566

6667
/** The Sentry Browser SDK Backend. */
6768
export class BrowserBackend implements Backend {
68-
/** Handle to the SDK frontend for callbacks. */
69-
private readonly frontend: Frontend<BrowserOptions>;
70-
7169
/** Creates a new browser backend instance. */
72-
public constructor(frontend: Frontend<BrowserOptions>) {
73-
this.frontend = frontend;
74-
}
70+
public constructor(private readonly options: BrowserOptions) {}
7571

7672
/**
7773
* @inheritDoc
7874
*/
7975
public install(): boolean {
80-
// We are only called by the frontend if the SDK is enabled and a valid DSN
76+
// We are only called by the client if the SDK is enabled and a valid DSN
8177
// has been configured. If no DSN is present, this indicates a programming
8278
// error.
83-
const dsn = this.frontend.getDSN();
79+
const dsn = this.options.dsn;
8480
if (!dsn) {
8581
throw new SentryError(
8682
'Invariant exception: install() must not be called when disabled',
8783
);
8884
}
8985

90-
Raven.config(dsn.toString(), this.frontend.getOptions()).install();
86+
Raven.config(dsn, this.options).install();
9187

9288
// Hook into Raven's breadcrumb mechanism. This allows us to intercept both
93-
// breadcrumbs created internally by Raven and pass them to the Frontend
89+
// breadcrumbs created internally by Raven and pass them to the Client
9490
// first, before actually capturing them.
9591
Raven.setBreadcrumbCallback(breadcrumb => {
9692
addBreadcrumb(breadcrumb);
9793
return false;
9894
});
9995

10096
// Hook into Raven's internal event sending mechanism. This allows us to
101-
// pass events to the frontend, before they will be sent back here for
97+
// pass events to the client, before they will be sent back here for
10298
// actual submission.
10399
Raven._sendProcessedPayload = event => {
104-
captureEvent(normalizeRavenEvent(event));
100+
const normalizedEvent = normalizeRavenEvent(event);
101+
if (normalizedEvent) {
102+
captureEvent(normalizedEvent);
103+
}
105104
};
106105

107106
return true;
@@ -117,9 +116,12 @@ export class BrowserBackend implements Backend {
117116
Raven._sendProcessedPayload = evt => {
118117
event = evt;
119118
};
120-
121119
Raven.captureException(exception);
122-
return normalizeRavenEvent(event);
120+
const normalizedEvent = normalizeRavenEvent(event);
121+
if (normalizedEvent) {
122+
return normalizedEvent;
123+
}
124+
throw new SentryError('Event was undefined when it should be an event');
123125
} finally {
124126
Raven._sendProcessedPayload = originalSend;
125127
}
@@ -135,9 +137,12 @@ export class BrowserBackend implements Backend {
135137
Raven._sendProcessedPayload = evt => {
136138
event = evt;
137139
};
138-
139140
Raven.captureMessage(message);
140-
return normalizeRavenEvent(event);
141+
const normalizedEvent = normalizeRavenEvent(event);
142+
if (normalizedEvent) {
143+
return normalizedEvent;
144+
}
145+
throw new SentryError('Event was undefined when it should be an event');
141146
} finally {
142147
Raven._sendProcessedPayload = originalSend;
143148
}

packages/browser/src/frontend.ts renamed to packages/browser/src/client.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import { FrontendBase } from '@sentry/core';
1+
import { BaseClient } from '@sentry/core';
22
import { SdkInfo } from '@sentry/shim';
33
import { BrowserBackend, BrowserOptions } from './backend';
44
import { Raven } from './raven';
55

66
/**
7-
* The Sentry Browser SDK Frontend.
7+
* The Sentry Browser SDK Client.
88
*
99
* @see BrowserOptions for documentation on configuration options.
1010
* @see SentryClient for usage documentation.
1111
*/
12-
export class BrowserFrontend extends FrontendBase<
13-
BrowserBackend,
14-
BrowserOptions
15-
> {
12+
export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
1613
/**
1714
* Creates a new Browser SDK instance.
1815
*

packages/browser/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ export {
2727
} from '@sentry/shim';
2828

2929
export { BrowserBackend, BrowserOptions } from './backend';
30-
export { BrowserFrontend } from './frontend';
31-
export { init, getCurrentFrontend } from './sdk';
30+
export { BrowserClient } from './client';
31+
export { init, getCurrentClient } from './sdk';

packages/browser/src/sdk.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { initAndBind } from '@sentry/core';
2-
import { getCurrentClient } from '@sentry/shim';
2+
import { getCurrentClient as shimGetCurrentClient } from '@sentry/shim';
33
import { BrowserOptions } from './backend';
4-
import { BrowserFrontend } from './frontend';
4+
import { BrowserClient } from './client';
55

66
/**
77
* The Sentry Browser SDK Client.
@@ -47,10 +47,10 @@ import { BrowserFrontend } from './frontend';
4747
* @see BrowserOptions for documentation on configuration options.
4848
*/
4949
export function init(options: BrowserOptions): void {
50-
initAndBind(BrowserFrontend, options);
50+
initAndBind(BrowserClient, options);
5151
}
5252

53-
/** Returns the current BrowserFrontend, if any. */
54-
export function getCurrentFrontend(): BrowserFrontend {
55-
return getCurrentClient() as BrowserFrontend;
53+
/** Returns the current BrowserClient, if any. */
54+
export function getCurrentClient(): BrowserClient {
55+
return shimGetCurrentClient() as BrowserClient;
5656
}

packages/browser/test/index.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { spy, stub } from 'sinon';
44
import {
55
addBreadcrumb,
66
BrowserBackend,
7-
BrowserFrontend,
7+
BrowserClient,
88
captureEvent,
99
captureException,
1010
captureMessage,
@@ -29,7 +29,7 @@ describe('SentryBrowser', () => {
2929
let s: sinon.SinonSpy;
3030

3131
beforeEach(() => {
32-
s = spy(BrowserFrontend.prototype, 'setContext');
32+
s = spy(BrowserClient.prototype, 'setContext');
3333
});
3434

3535
afterEach(() => {
@@ -70,7 +70,7 @@ describe('SentryBrowser', () => {
7070

7171
it('should record auto breadcrumbs', done => {
7272
pushScope(
73-
new BrowserFrontend({
73+
new BrowserClient({
7474
afterSend: (event: SentryEvent) => {
7575
expect(event.breadcrumbs!).to.have.lengthOf(3);
7676
done();
@@ -110,7 +110,7 @@ describe('SentryBrowser', () => {
110110

111111
it('should capture an exception', done => {
112112
pushScope(
113-
new BrowserFrontend({
113+
new BrowserClient({
114114
afterSend: (event: SentryEvent) => {
115115
expect(event.exception).to.not.be.undefined;
116116
expect(event.exception![0]).to.not.be.undefined;
@@ -132,7 +132,7 @@ describe('SentryBrowser', () => {
132132

133133
it('should capture a message', done => {
134134
pushScope(
135-
new BrowserFrontend({
135+
new BrowserClient({
136136
afterSend: (event: SentryEvent) => {
137137
expect(event.message).to.equal('test');
138138
expect(event.exception).to.be.undefined;
@@ -147,7 +147,7 @@ describe('SentryBrowser', () => {
147147

148148
it('should capture an event', done => {
149149
pushScope(
150-
new BrowserFrontend({
150+
new BrowserClient({
151151
afterSend: (event: SentryEvent) => {
152152
expect(event.message).to.equal('test');
153153
expect(event.exception).to.be.undefined;

packages/core/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ building Sentry JavaScript SDKs, like `@sentry/node` or `@sentry/browser`.
1818

1919
TODO
2020

21-
## Specializing the Frontend
21+
## Specializing the Client
2222

2323
TODO
2424

packages/core/src/base.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Breadcrumb, Context, SdkInfo, SentryEvent } from '@sentry/shim';
22
import { DSN } from './dsn';
3-
import { Backend, Frontend, Options, Scope } from './interfaces';
3+
import { Backend, Client, Options, Scope } from './interfaces';
44
import { SendStatus } from './status';
55

66
/**
@@ -17,49 +17,49 @@ const MAX_BREADCRUMBS = 100;
1717

1818
/** A class object that can instanciate Backend objects. */
1919
export interface BackendClass<B extends Backend, O extends Options> {
20-
new (frontend: Frontend<O>): B;
20+
new (options: O): B;
2121
}
2222

2323
/**
24-
* Base implementation for all JavaScript SDK frontends.
24+
* Base implementation for all JavaScript SDK clients.
2525
*
2626
* Call the constructor with the corresponding backend constructor and options
27-
* specific to the frontend subclass. To access these options later, use
28-
* {@link Frontend.getOptions}. Also, the Backend instance is available via
29-
* {@link Frontend.getBackend}.
27+
* specific to the client subclass. To access these options later, use
28+
* {@link Client.getOptions}. Also, the Backend instance is available via
29+
* {@link Client.getBackend}.
3030
*
3131
* Subclasses must implement one abstract method: {@link getSdkInfo}. It must
3232
* return the unique name and the version of the SDK.
3333
*
3434
* If a DSN is specified in the options, it will be parsed and stored. Use
35-
* {@link Frontend.getDSN} to retrieve the DSN at any moment. In case the DSN is
35+
* {@link Client.getDSN} to retrieve the DSN at any moment. In case the DSN is
3636
* invalid, the constructor will throw a {@link SentryException}. Note that
3737
* without a valid DSN, the SDK will not send any events to Sentry.
3838
*
3939
* Before sending an event via the backend, it is passed through
40-
* {@link FrontendBase.prepareEvent} to add SDK information and scope data
40+
* {@link BaseClient.prepareEvent} to add SDK information and scope data
4141
* (breadcrumbs and context). To add more custom information, override this
4242
* method and extend the resulting prepared event.
4343
*
4444
* To issue automatically created events (e.g. via instrumentation), use
45-
* {@link Frontend.captureEvent}. It will prepare the event and pass it through
45+
* {@link Client.captureEvent}. It will prepare the event and pass it through
4646
* the callback lifecycle. To issue auto-breadcrumbs, use
47-
* {@link Frontend.addBreadcrumb}.
47+
* {@link Client.addBreadcrumb}.
4848
*
4949
* @example
50-
* class NodeFrontend extends FrontendBase<NodeBackend, NodeOptions> {
50+
* class NodeClient extends BaseClient<NodeBackend, NodeOptions> {
5151
* public constructor(options: NodeOptions) {
5252
* super(NodeBackend, options);
5353
* }
5454
*
5555
* // ...
5656
* }
5757
*/
58-
export abstract class FrontendBase<B extends Backend, O extends Options>
59-
implements Frontend<O> {
58+
export abstract class BaseClient<B extends Backend, O extends Options>
59+
implements Client<O> {
6060
/**
6161
* The backend used to physically interact in the enviornment. Usually, this
62-
* will correspond to the frontend. When composing SDKs, however, the Backend
62+
* will correspond to the client. When composing SDKs, however, the Backend
6363
* from the root SDK will be used.
6464
*/
6565
private readonly backend: B;
@@ -76,7 +76,7 @@ export abstract class FrontendBase<B extends Backend, O extends Options>
7676
/**
7777
* A scope instance containing breadcrumbs and context, used if none is
7878
* specified to the public methods. This is specifically used in standalone
79-
* mode, when the Frontend is directly instanciated by the user.
79+
* mode, when the Client is directly instanciated by the user.
8080
*/
8181
private readonly internalScope: Scope;
8282

@@ -87,13 +87,13 @@ export abstract class FrontendBase<B extends Backend, O extends Options>
8787
private installed?: boolean;
8888

8989
/**
90-
* Initializes this frontend instance.
90+
* Initializes this client instance.
9191
*
9292
* @param backendClass A constructor function to create the backend.
93-
* @param options Options for the frontend.
93+
* @param options Options for the client.
9494
*/
9595
protected constructor(backendClass: BackendClass<B, O>, options: O) {
96-
this.backend = new backendClass(this);
96+
this.backend = new backendClass(options);
9797
this.options = options;
9898

9999
if (options.dsn) {
@@ -259,7 +259,7 @@ export abstract class FrontendBase<B extends Backend, O extends Options>
259259
* Adds common information to events.
260260
*
261261
* The information includes release and environment from `options`, SDK
262-
* information returned by {@link FrontendBase.getSdkInfo}, as well as
262+
* information returned by {@link BaseClient.getSdkInfo}, as well as
263263
* breadcrumbs and context (extra, tags and user) from the scope.
264264
*
265265
* Information that is already present in the event is never overwritten. For

packages/core/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export { BackendClass, FrontendBase } from './base';
1+
export { BackendClass, BaseClient } from './base';
22
export { DSN, DSNComponents, DSNLike, DSNProtocol } from './dsn';
33
export { SentryError } from './error';
4-
export { Backend, Frontend, LogLevel, Options, Scope } from './interfaces';
5-
export { initAndBind, FrontendClass } from './sdk';
4+
export { Backend, Client, LogLevel, Options, Scope } from './interfaces';
5+
export { initAndBind, ClientClass } from './sdk';

0 commit comments

Comments
 (0)