From 120b19c6a88b8ecb122c8a0dc69ab8865122a3b7 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 25 Apr 2022 11:46:18 -0400 Subject: [PATCH] feat(hub): Remove _invokeClient --- packages/hub/src/hub.ts | 48 ++++++++++++++++++++++------------- packages/hub/test/hub.test.ts | 7 ----- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/packages/hub/src/hub.ts b/packages/hub/src/hub.ts index 4e97cd8aa9b3..ac98e053faf5 100644 --- a/packages/hub/src/hub.ts +++ b/packages/hub/src/hub.ts @@ -185,11 +185,18 @@ export class Hub implements HubInterface { // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public captureException(exception: any, hint?: EventHint): string { const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4()); - this._invokeClient('captureException', exception, { - originalException: exception, - syntheticException: new Error('Sentry syntheticException'), - ...hint, - event_id: eventId, + const syntheticException = new Error('Sentry syntheticException'); + this._withClient((client, scope) => { + client.captureException( + exception, + { + originalException: exception, + syntheticException, + ...hint, + event_id: eventId, + }, + scope, + ); }); return eventId; } @@ -204,11 +211,19 @@ export class Hub implements HubInterface { hint?: EventHint, ): string { const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4()); - this._invokeClient('captureMessage', message, level, { - originalException: message, - syntheticException: new Error(message), - ...hint, - event_id: eventId, + const syntheticException = new Error(message); + this._withClient((client, scope) => { + client.captureMessage( + message, + level, + { + originalException: message, + syntheticException, + ...hint, + event_id: eventId, + }, + scope, + ); }); return eventId; } @@ -222,9 +237,8 @@ export class Hub implements HubInterface { this._lastEventId = eventId; } - this._invokeClient('captureEvent', event, { - ...hint, - event_id: eventId, + this._withClient((client, scope) => { + client.captureEvent(event, { ...hint, event_id: eventId }, scope); }); return eventId; } @@ -446,12 +460,10 @@ export class Hub implements HubInterface { * @param method The method to call on the client. * @param args Arguments to pass to the client function. */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - private _invokeClient(method: M, ...args: any[]): void { + private _withClient(callback: (client: Client, scope: Scope | undefined) => void): void { const { scope, client } = this.getStackTop(); - if (client && client[method]) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any - (client as any)[method](...args, scope); + if (client) { + callback(client, scope); } } diff --git a/packages/hub/test/hub.test.ts b/packages/hub/test/hub.test.ts index 6754ac258669..443f56b19e11 100644 --- a/packages/hub/test/hub.test.ts +++ b/packages/hub/test/hub.test.ts @@ -52,13 +52,6 @@ describe('Hub', () => { expect(hub.getStack()).toHaveLength(1); }); - test("don't invoke client sync with wrong func", () => { - const hub = new Hub(clientFn); - // @ts-ignore we want to able to call private method - hub._invokeClient('funca', true); - expect(clientFn).not.toHaveBeenCalled(); - }); - test('isOlderThan', () => { const hub = new Hub(); expect(hub.isOlderThan(0)).toBeFalsy();