Skip to content

Commit c930bf0

Browse files
committed
port CaptureException
1 parent 6267b54 commit c930bf0

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

packages/hub/src/exports.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { CaptureContext, Hub } from '@sentry/types';
2+
3+
import { getCurrentHub } from './hub';
4+
5+
/**
6+
* This calls a function on the current hub.
7+
* @param method function to call on hub.
8+
* @param args to pass to function.
9+
*/
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
export function callOnHub<T>(method: string, ...args: any[]): T {
12+
const hub = getCurrentHub();
13+
if (hub && hub[method as keyof Hub]) {
14+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15+
return (hub[method as keyof Hub] as any)(...args);
16+
}
17+
throw new Error(`No hub defined or ${method} was not found on the hub, please open a bug report.`);
18+
}
19+
20+
/**
21+
* Captures an exception event and sends it to Sentry.
22+
*
23+
* @param exception An exception-like object.
24+
* @returns The generated eventId.
25+
*/
26+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
27+
export function captureException(exception: any, captureContext?: CaptureContext): string {
28+
return getCurrentHub().captureException(exception, { captureContext });
29+
}

packages/hub/src/hub.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,27 +185,17 @@ 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-
let finalHint = hint;
189188

190-
// If there's no explicit hint provided, mimic the same thing that would happen
191-
// in the minimal itself to create a consistent behavior.
192-
// We don't do this in the client, as it's the lowest level API, and doing this,
193-
// would prevent user from having full control over direct calls.
194-
if (!hint) {
195-
let syntheticException: Error;
196-
try {
197-
throw new Error('Sentry syntheticException');
198-
} catch (exception) {
199-
syntheticException = exception as Error;
200-
}
201-
finalHint = {
202-
originalException: exception,
203-
syntheticException,
204-
};
189+
let syntheticException: Error;
190+
try {
191+
throw new Error('Sentry syntheticException');
192+
} catch (exception) {
193+
syntheticException = exception as Error;
205194
}
206-
207195
this._invokeClient('captureException', exception, {
208-
...finalHint,
196+
originalException: exception,
197+
syntheticException,
198+
...hint,
209199
event_id: eventId,
210200
});
211201
return eventId;

packages/minimal/src/index.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,6 @@ function callOnHub<T>(method: string, ...args: any[]): T {
2929
throw new Error(`No hub defined or ${method} was not found on the hub, please open a bug report.`);
3030
}
3131

32-
/**
33-
* Captures an exception event and sends it to Sentry.
34-
*
35-
* @param exception An exception-like object.
36-
* @returns The generated eventId.
37-
*/
38-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
39-
export function captureException(exception: any, captureContext?: CaptureContext): string {
40-
const syntheticException = new Error('Sentry syntheticException');
41-
42-
return callOnHub('captureException', exception, {
43-
captureContext,
44-
originalException: exception,
45-
syntheticException,
46-
});
47-
}
48-
4932
/**
5033
* Captures a message event and sends it to Sentry.
5134
*

0 commit comments

Comments
 (0)