Skip to content

Commit b3fddcd

Browse files
authored
feat(hub): Remove _invokeClient (#4972)
This PR removes the dynamic dispatch of `_invokeClient` in favour of explicitly using client methods. We introduce a `_withClient` helper that does this. `_withClient` grabs the client and scope from the stack of scopes on the hub, and executes a callback with those values. This allows the consumer of the function to directly access the client instance and call methods on it.
1 parent 389f4ee commit b3fddcd

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

packages/hub/src/hub.ts

+30-18
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,18 @@ export class Hub implements HubInterface {
185185
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
186186
public captureException(exception: any, hint?: EventHint): string {
187187
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
188-
this._invokeClient('captureException', exception, {
189-
originalException: exception,
190-
syntheticException: new Error('Sentry syntheticException'),
191-
...hint,
192-
event_id: eventId,
188+
const syntheticException = new Error('Sentry syntheticException');
189+
this._withClient((client, scope) => {
190+
client.captureException(
191+
exception,
192+
{
193+
originalException: exception,
194+
syntheticException,
195+
...hint,
196+
event_id: eventId,
197+
},
198+
scope,
199+
);
193200
});
194201
return eventId;
195202
}
@@ -204,11 +211,19 @@ export class Hub implements HubInterface {
204211
hint?: EventHint,
205212
): string {
206213
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
207-
this._invokeClient('captureMessage', message, level, {
208-
originalException: message,
209-
syntheticException: new Error(message),
210-
...hint,
211-
event_id: eventId,
214+
const syntheticException = new Error(message);
215+
this._withClient((client, scope) => {
216+
client.captureMessage(
217+
message,
218+
level,
219+
{
220+
originalException: message,
221+
syntheticException,
222+
...hint,
223+
event_id: eventId,
224+
},
225+
scope,
226+
);
212227
});
213228
return eventId;
214229
}
@@ -222,9 +237,8 @@ export class Hub implements HubInterface {
222237
this._lastEventId = eventId;
223238
}
224239

225-
this._invokeClient('captureEvent', event, {
226-
...hint,
227-
event_id: eventId,
240+
this._withClient((client, scope) => {
241+
client.captureEvent(event, { ...hint, event_id: eventId }, scope);
228242
});
229243
return eventId;
230244
}
@@ -446,12 +460,10 @@ export class Hub implements HubInterface {
446460
* @param method The method to call on the client.
447461
* @param args Arguments to pass to the client function.
448462
*/
449-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
450-
private _invokeClient<M extends keyof Client>(method: M, ...args: any[]): void {
463+
private _withClient(callback: (client: Client, scope: Scope | undefined) => void): void {
451464
const { scope, client } = this.getStackTop();
452-
if (client && client[method]) {
453-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
454-
(client as any)[method](...args, scope);
465+
if (client) {
466+
callback(client, scope);
455467
}
456468
}
457469

packages/hub/test/hub.test.ts

-7
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ describe('Hub', () => {
5252
expect(hub.getStack()).toHaveLength(1);
5353
});
5454

55-
test("don't invoke client sync with wrong func", () => {
56-
const hub = new Hub(clientFn);
57-
// @ts-ignore we want to able to call private method
58-
hub._invokeClient('funca', true);
59-
expect(clientFn).not.toHaveBeenCalled();
60-
});
61-
6255
test('isOlderThan', () => {
6356
const hub = new Hub();
6457
expect(hub.isOlderThan(0)).toBeFalsy();

0 commit comments

Comments
 (0)