From 681838dfbb69222eb75da343d1b2f1c7c252de12 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Tue, 19 Mar 2019 11:08:34 +0100 Subject: [PATCH] feat: Use generic for getClient on hub --- .../browser/src/integrations/breadcrumbs.ts | 6 +++--- packages/browser/src/sdk.ts | 17 ++++++++++++++--- .../core/src/integrations/inboundfilters.ts | 4 ++-- packages/hub/src/hub.ts | 4 ++-- packages/node/src/handlers.ts | 12 ++++++++++-- .../src/integrations/onuncaughtexception.ts | 7 ++++--- packages/node/src/sdk.ts | 12 ++++++++++-- packages/node/test/onunhandledrejection.test.ts | 8 +++++--- 8 files changed, 50 insertions(+), 20 deletions(-) diff --git a/packages/browser/src/integrations/breadcrumbs.ts b/packages/browser/src/integrations/breadcrumbs.ts index 382b453e1adf..d09f15689518 100644 --- a/packages/browser/src/integrations/breadcrumbs.ts +++ b/packages/browser/src/integrations/breadcrumbs.ts @@ -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(); const dsn = client && client.getDsn(); if (dsn) { const filterUrl = new API(dsn).getStoreEndpoint(); @@ -205,7 +205,7 @@ export class Breadcrumbs implements Integration { method = args[1].method; } - const client = getCurrentHub().getClient() as BrowserClient; + const client = getCurrentHub().getClient(); const dsn = client && client.getDsn(); if (dsn) { const filterUrl = new API(dsn).getStoreEndpoint(); @@ -376,7 +376,7 @@ export class Breadcrumbs implements Integration { url: args[1], }; - const client = getCurrentHub().getClient() as BrowserClient; + const client = getCurrentHub().getClient(); const dsn = client && client.getDsn(); if (dsn) { const filterUrl = new API(dsn).getStoreEndpoint(); diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index 25b6402dc20f..8fbedee0c10b 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -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(); + if (client) { + client.showReportDialog(options); + } } /** @@ -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 { - return (getCurrentHub().getClient() as BrowserClient).flush(timeout); + const client = getCurrentHub().getClient(); + if (client) { + return client.flush(timeout); + } + return Promise.reject(false); } /** @@ -132,5 +139,9 @@ export async function flush(timeout?: number): Promise { * @param timeout Maximum time in ms the client should wait. */ export async function close(timeout?: number): Promise { - return (getCurrentHub().getClient() as BrowserClient).close(timeout); + const client = getCurrentHub().getClient(); + if (client) { + return client.close(timeout); + } + return Promise.reject(false); } diff --git a/packages/core/src/integrations/inboundfilters.ts b/packages/core/src/integrations/inboundfilters.ts index 0e132c53d554..185419ad12b3 100644 --- a/packages/core/src/integrations/inboundfilters.ts +++ b/packages/core/src/integrations/inboundfilters.ts @@ -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'; @@ -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)) { diff --git a/packages/hub/src/hub.ts b/packages/hub/src/hub.ts index 4be3b24495b0..8ba2ea26e658 100644 --- a/packages/hub/src/hub.ts +++ b/packages/hub/src/hub.ts @@ -134,8 +134,8 @@ export class Hub implements HubInterface { /** * @inheritDoc */ - public getClient(): Client | undefined { - return this.getStackTop().client; + public getClient(): C | undefined { + return this.getStackTop().client as C; } /** Returns the scope of the top stack. */ diff --git a/packages/node/src/handlers.ts b/packages/node/src/handlers.ts index 61fcba271b84..40fd3227f6ae 100644 --- a/packages/node/src/handlers.ts +++ b/packages/node/src/handlers.ts @@ -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(); + + 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!'); } diff --git a/packages/node/src/integrations/onuncaughtexception.ts b/packages/node/src/integrations/onuncaughtexception.ts index 281913c545af..7e02845dcbee 100644 --- a/packages/node/src/integrations/onuncaughtexception.ts +++ b/packages/node/src/integrations/onuncaughtexception.ts @@ -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 */ @@ -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(); 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) { diff --git a/packages/node/src/sdk.ts b/packages/node/src/sdk.ts index 0108851e1863..a8cdbdf98aca 100644 --- a/packages/node/src/sdk.ts +++ b/packages/node/src/sdk.ts @@ -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 { - return (getCurrentHub().getClient() as NodeClient).flush(timeout); + const client = getCurrentHub().getClient(); + if (client) { + return client.flush(timeout); + } + return Promise.reject(false); } /** @@ -125,5 +129,9 @@ export async function flush(timeout?: number): Promise { * @param timeout Maximum time in ms the client should wait. */ export async function close(timeout?: number): Promise { - return (getCurrentHub().getClient() as NodeClient).close(timeout); + const client = getCurrentHub().getClient(); + if (client) { + return client.close(timeout); + } + return Promise.reject(false); } diff --git a/packages/node/test/onunhandledrejection.test.ts b/packages/node/test/onunhandledrejection.test.ts index 84208b1b90d2..7290db52c9ad 100644 --- a/packages/node/test/onunhandledrejection.test.ts +++ b/packages/node/test/onunhandledrejection.test.ts @@ -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' }]); }); });