Skip to content

Commit 681838d

Browse files
committed
feat: Use generic for getClient on hub
1 parent 41c0f4e commit 681838d

File tree

8 files changed

+50
-20
lines changed

8 files changed

+50
-20
lines changed

packages/browser/src/integrations/breadcrumbs.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class Breadcrumbs implements Integration {
8787
// https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API/Using_the_Beacon_API
8888
const result = originalBeaconFunction.apply(this, args);
8989

90-
const client = getCurrentHub().getClient() as BrowserClient;
90+
const client = getCurrentHub().getClient<BrowserClient>();
9191
const dsn = client && client.getDsn();
9292
if (dsn) {
9393
const filterUrl = new API(dsn).getStoreEndpoint();
@@ -205,7 +205,7 @@ export class Breadcrumbs implements Integration {
205205
method = args[1].method;
206206
}
207207

208-
const client = getCurrentHub().getClient() as BrowserClient;
208+
const client = getCurrentHub().getClient<BrowserClient>();
209209
const dsn = client && client.getDsn();
210210
if (dsn) {
211211
const filterUrl = new API(dsn).getStoreEndpoint();
@@ -376,7 +376,7 @@ export class Breadcrumbs implements Integration {
376376
url: args[1],
377377
};
378378

379-
const client = getCurrentHub().getClient() as BrowserClient;
379+
const client = getCurrentHub().getClient<BrowserClient>();
380380
const dsn = client && client.getDsn();
381381
if (dsn) {
382382
const filterUrl = new API(dsn).getStoreEndpoint();

packages/browser/src/sdk.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ export function showReportDialog(options: ReportDialogOptions = {}): void {
8787
if (!options.eventId) {
8888
options.eventId = getCurrentHub().lastEventId();
8989
}
90-
(getCurrentHub().getClient() as BrowserClient).showReportDialog(options);
90+
const client = getCurrentHub().getClient<BrowserClient>();
91+
if (client) {
92+
client.showReportDialog(options);
93+
}
9194
}
9295

9396
/**
@@ -122,7 +125,11 @@ export function onLoad(callback: () => void): void {
122125
* @param timeout Maximum time in ms the client should wait.
123126
*/
124127
export async function flush(timeout?: number): Promise<boolean> {
125-
return (getCurrentHub().getClient() as BrowserClient).flush(timeout);
128+
const client = getCurrentHub().getClient<BrowserClient>();
129+
if (client) {
130+
return client.flush(timeout);
131+
}
132+
return Promise.reject(false);
126133
}
127134

128135
/**
@@ -132,5 +139,9 @@ export async function flush(timeout?: number): Promise<boolean> {
132139
* @param timeout Maximum time in ms the client should wait.
133140
*/
134141
export async function close(timeout?: number): Promise<boolean> {
135-
return (getCurrentHub().getClient() as BrowserClient).close(timeout);
142+
const client = getCurrentHub().getClient<BrowserClient>();
143+
if (client) {
144+
return client.close(timeout);
145+
}
146+
return Promise.reject(false);
136147
}

packages/core/src/integrations/inboundfilters.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
2-
import { Client, Event, Integration } from '@sentry/types';
2+
import { Event, Integration } from '@sentry/types';
33
import { isRegExp } from '@sentry/utils/is';
44
import { logger } from '@sentry/utils/logger';
55
import { getEventDescription } from '@sentry/utils/misc';
@@ -40,7 +40,7 @@ export class InboundFilters implements Integration {
4040
}
4141
const self = hub.getIntegration(InboundFilters);
4242
if (self) {
43-
const client = hub.getClient() as Client;
43+
const client = hub.getClient();
4444
const clientOptions = client ? client.getOptions() : {};
4545
const options = self._mergeOptions(clientOptions);
4646
if (self._shouldDropEvent(event, options)) {

packages/hub/src/hub.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export class Hub implements HubInterface {
134134
/**
135135
* @inheritDoc
136136
*/
137-
public getClient(): Client | undefined {
138-
return this.getStackTop().client;
137+
public getClient<C extends Client>(): C | undefined {
138+
return this.getStackTop().client as C;
139139
}
140140

141141
/** Returns the scope of the top stack. */

packages/node/src/handlers.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,20 @@ export function errorHandler(): (
291291
*/
292292
export function defaultOnFatalError(error: Error): void {
293293
console.error(error && error.stack ? error.stack : error);
294-
const options = (getCurrentHub().getClient() as NodeClient).getOptions();
294+
const client = getCurrentHub().getClient<NodeClient>();
295+
296+
if (client === undefined) {
297+
logger.warn('No NodeClient was defined, we are exiting the process now.');
298+
global.process.exit(1);
299+
return;
300+
}
301+
302+
const options = client.getOptions();
295303
const timeout =
296304
(options && options.shutdownTimeout && options.shutdownTimeout > 0 && options.shutdownTimeout) ||
297305
DEFAULT_SHUTDOWN_TIMEOUT;
298306
forget(
299-
(getCurrentHub().getClient() as NodeClient).close(timeout).then((result: boolean) => {
307+
client.close(timeout).then((result: boolean) => {
300308
if (!result) {
301309
logger.warn('We reached the timeout for emptying the request buffer, still exiting now!');
302310
}

packages/node/src/integrations/onuncaughtexception.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Integration, Severity } from '@sentry/types';
33
import { logger } from '@sentry/utils/logger';
44

55
import { NodeOptions } from '../backend';
6+
import { NodeClient } from '../client';
67
import { defaultOnFatalError } from '../handlers';
78

89
/** Global Promise Rejection handler */
@@ -55,12 +56,12 @@ export class OnUncaughtException implements Integration {
5556
type onFatalErrorHandlerType = (firstError: Error, secondError?: Error) => void;
5657

5758
let onFatalError: onFatalErrorHandlerType = defaultOnFatalError;
58-
const client = getCurrentHub().getClient();
59+
const client = getCurrentHub().getClient<NodeClient>();
5960

6061
if (this._options.onFatalError) {
6162
onFatalError = this._options.onFatalError;
62-
} else if (client && (client.getOptions() as NodeOptions).onFatalError) {
63-
onFatalError = (client.getOptions() as NodeOptions).onFatalError as onFatalErrorHandlerType;
63+
} else if (client && client.getOptions().onFatalError) {
64+
onFatalError = client.getOptions().onFatalError as onFatalErrorHandlerType;
6465
}
6566

6667
if (!caughtFirstError) {

packages/node/src/sdk.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ export function lastEventId(): string | undefined {
115115
* @param timeout Maximum time in ms the client should wait.
116116
*/
117117
export async function flush(timeout?: number): Promise<boolean> {
118-
return (getCurrentHub().getClient() as NodeClient).flush(timeout);
118+
const client = getCurrentHub().getClient<NodeClient>();
119+
if (client) {
120+
return client.flush(timeout);
121+
}
122+
return Promise.reject(false);
119123
}
120124

121125
/**
@@ -125,5 +129,9 @@ export async function flush(timeout?: number): Promise<boolean> {
125129
* @param timeout Maximum time in ms the client should wait.
126130
*/
127131
export async function close(timeout?: number): Promise<boolean> {
128-
return (getCurrentHub().getClient() as NodeClient).close(timeout);
132+
const client = getCurrentHub().getClient<NodeClient>();
133+
if (client) {
134+
return client.close(timeout);
135+
}
136+
return Promise.reject(false);
129137
}

packages/node/test/onunhandledrejection.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ describe('unhandled promises', () => {
2323
const captureException = jest.spyOn(Hub.prototype, 'captureException');
2424
const setUser = jest.spyOn(Scope.prototype, 'setUser');
2525
const setExtra = jest.spyOn(Scope.prototype, 'setExtra');
26-
const setTag = jest.spyOn(Scope.prototype, 'setTag');
26+
const setExtras = jest.spyOn(Scope.prototype, 'setExtras');
27+
const setTags = jest.spyOn(Scope.prototype, 'setTags');
2728

2829
integration.sendUnhandledPromise('bla', promise);
2930

3031
expect(captureException.mock.calls[0][0]).toBe('bla');
3132
expect(setUser.mock.calls[0][0]).toEqual({ id: 1 });
3233
expect(setExtra.mock.calls[0]).toEqual(['unhandledPromiseRejection', true]);
33-
expect(setExtra.mock.calls[1]).toEqual(['extra', '1']);
34-
expect(setTag.mock.calls[0]).toEqual(['tag', '2']);
34+
35+
expect(setExtras.mock.calls[0]).toEqual([{ extra: '1' }]);
36+
expect(setTags.mock.calls[0]).toEqual([{ tag: '2' }]);
3537
});
3638
});

0 commit comments

Comments
 (0)