Skip to content

Commit

Permalink
feat: Use generic for getClient on hub
Browse files Browse the repository at this point in the history
  • Loading branch information
HazAT committed Mar 20, 2019
1 parent 41c0f4e commit 681838d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 20 deletions.
6 changes: 3 additions & 3 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Breadcrumbs implements Integration {
// https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API/Using_the_Beacon_API
const result = originalBeaconFunction.apply(this, args);

const client = getCurrentHub().getClient() as BrowserClient;
const client = getCurrentHub().getClient<BrowserClient>();
const dsn = client && client.getDsn();
if (dsn) {
const filterUrl = new API(dsn).getStoreEndpoint();
Expand Down Expand Up @@ -205,7 +205,7 @@ export class Breadcrumbs implements Integration {
method = args[1].method;
}

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

const client = getCurrentHub().getClient() as BrowserClient;
const client = getCurrentHub().getClient<BrowserClient>();
const dsn = client && client.getDsn();
if (dsn) {
const filterUrl = new API(dsn).getStoreEndpoint();
Expand Down
17 changes: 14 additions & 3 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export function showReportDialog(options: ReportDialogOptions = {}): void {
if (!options.eventId) {
options.eventId = getCurrentHub().lastEventId();
}
(getCurrentHub().getClient() as BrowserClient).showReportDialog(options);
const client = getCurrentHub().getClient<BrowserClient>();
if (client) {
client.showReportDialog(options);
}
}

/**
Expand Down Expand Up @@ -122,7 +125,11 @@ export function onLoad(callback: () => void): void {
* @param timeout Maximum time in ms the client should wait.
*/
export async function flush(timeout?: number): Promise<boolean> {
return (getCurrentHub().getClient() as BrowserClient).flush(timeout);
const client = getCurrentHub().getClient<BrowserClient>();
if (client) {
return client.flush(timeout);
}
return Promise.reject(false);
}

/**
Expand All @@ -132,5 +139,9 @@ export async function flush(timeout?: number): Promise<boolean> {
* @param timeout Maximum time in ms the client should wait.
*/
export async function close(timeout?: number): Promise<boolean> {
return (getCurrentHub().getClient() as BrowserClient).close(timeout);
const client = getCurrentHub().getClient<BrowserClient>();
if (client) {
return client.close(timeout);
}
return Promise.reject(false);
}
4 changes: 2 additions & 2 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
import { Client, Event, Integration } from '@sentry/types';
import { Event, Integration } from '@sentry/types';
import { isRegExp } from '@sentry/utils/is';
import { logger } from '@sentry/utils/logger';
import { getEventDescription } from '@sentry/utils/misc';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class InboundFilters implements Integration {
}
const self = hub.getIntegration(InboundFilters);
if (self) {
const client = hub.getClient() as Client;
const client = hub.getClient();
const clientOptions = client ? client.getOptions() : {};
const options = self._mergeOptions(clientOptions);
if (self._shouldDropEvent(event, options)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ export class Hub implements HubInterface {
/**
* @inheritDoc
*/
public getClient(): Client | undefined {
return this.getStackTop().client;
public getClient<C extends Client>(): C | undefined {
return this.getStackTop().client as C;
}

/** Returns the scope of the top stack. */
Expand Down
12 changes: 10 additions & 2 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,20 @@ export function errorHandler(): (
*/
export function defaultOnFatalError(error: Error): void {
console.error(error && error.stack ? error.stack : error);
const options = (getCurrentHub().getClient() as NodeClient).getOptions();
const client = getCurrentHub().getClient<NodeClient>();

if (client === undefined) {
logger.warn('No NodeClient was defined, we are exiting the process now.');
global.process.exit(1);
return;
}

const options = client.getOptions();
const timeout =
(options && options.shutdownTimeout && options.shutdownTimeout > 0 && options.shutdownTimeout) ||
DEFAULT_SHUTDOWN_TIMEOUT;
forget(
(getCurrentHub().getClient() as NodeClient).close(timeout).then((result: boolean) => {
client.close(timeout).then((result: boolean) => {
if (!result) {
logger.warn('We reached the timeout for emptying the request buffer, still exiting now!');
}
Expand Down
7 changes: 4 additions & 3 deletions packages/node/src/integrations/onuncaughtexception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Integration, Severity } from '@sentry/types';
import { logger } from '@sentry/utils/logger';

import { NodeOptions } from '../backend';
import { NodeClient } from '../client';
import { defaultOnFatalError } from '../handlers';

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

let onFatalError: onFatalErrorHandlerType = defaultOnFatalError;
const client = getCurrentHub().getClient();
const client = getCurrentHub().getClient<NodeClient>();

if (this._options.onFatalError) {
onFatalError = this._options.onFatalError;
} else if (client && (client.getOptions() as NodeOptions).onFatalError) {
onFatalError = (client.getOptions() as NodeOptions).onFatalError as onFatalErrorHandlerType;
} else if (client && client.getOptions().onFatalError) {
onFatalError = client.getOptions().onFatalError as onFatalErrorHandlerType;
}

if (!caughtFirstError) {
Expand Down
12 changes: 10 additions & 2 deletions packages/node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ export function lastEventId(): string | undefined {
* @param timeout Maximum time in ms the client should wait.
*/
export async function flush(timeout?: number): Promise<boolean> {
return (getCurrentHub().getClient() as NodeClient).flush(timeout);
const client = getCurrentHub().getClient<NodeClient>();
if (client) {
return client.flush(timeout);
}
return Promise.reject(false);
}

/**
Expand All @@ -125,5 +129,9 @@ export async function flush(timeout?: number): Promise<boolean> {
* @param timeout Maximum time in ms the client should wait.
*/
export async function close(timeout?: number): Promise<boolean> {
return (getCurrentHub().getClient() as NodeClient).close(timeout);
const client = getCurrentHub().getClient<NodeClient>();
if (client) {
return client.close(timeout);
}
return Promise.reject(false);
}
8 changes: 5 additions & 3 deletions packages/node/test/onunhandledrejection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ describe('unhandled promises', () => {
const captureException = jest.spyOn(Hub.prototype, 'captureException');
const setUser = jest.spyOn(Scope.prototype, 'setUser');
const setExtra = jest.spyOn(Scope.prototype, 'setExtra');
const setTag = jest.spyOn(Scope.prototype, 'setTag');
const setExtras = jest.spyOn(Scope.prototype, 'setExtras');
const setTags = jest.spyOn(Scope.prototype, 'setTags');

integration.sendUnhandledPromise('bla', promise);

expect(captureException.mock.calls[0][0]).toBe('bla');
expect(setUser.mock.calls[0][0]).toEqual({ id: 1 });
expect(setExtra.mock.calls[0]).toEqual(['unhandledPromiseRejection', true]);
expect(setExtra.mock.calls[1]).toEqual(['extra', '1']);
expect(setTag.mock.calls[0]).toEqual(['tag', '2']);

expect(setExtras.mock.calls[0]).toEqual([{ extra: '1' }]);
expect(setTags.mock.calls[0]).toEqual([{ tag: '2' }]);
});
});

0 comments on commit 681838d

Please sign in to comment.