Skip to content

Commit b4b7221

Browse files
committed
port captureMessage
1 parent c930bf0 commit b4b7221

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

packages/hub/src/exports.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CaptureContext, Hub } from '@sentry/types';
1+
import { CaptureContext, Hub, Severity, SeverityLevel } from '@sentry/types';
22

33
import { getCurrentHub } from './hub';
44

@@ -27,3 +27,22 @@ export function callOnHub<T>(method: string, ...args: any[]): T {
2727
export function captureException(exception: any, captureContext?: CaptureContext): string {
2828
return getCurrentHub().captureException(exception, { captureContext });
2929
}
30+
31+
/**
32+
* Captures a message event and sends it to Sentry.
33+
*
34+
* @param message The message to send to Sentry.
35+
* @param Severity Define the level of the message.
36+
* @returns The generated eventId.
37+
*/
38+
export function captureMessage(
39+
message: string,
40+
// eslint-disable-next-line deprecation/deprecation
41+
captureContext?: CaptureContext | Severity | SeverityLevel,
42+
): string {
43+
// This is necessary to provide explicit scopes upgrade, without changing the original
44+
// arity of the `captureMessage(message, level)` method.
45+
const level = typeof captureContext === 'string' ? captureContext : undefined;
46+
const context = typeof captureContext !== 'string' ? { captureContext } : undefined;
47+
return getCurrentHub().captureMessage(message, level, context);
48+
}

packages/hub/src/hub.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,27 +211,18 @@ export class Hub implements HubInterface {
211211
hint?: EventHint,
212212
): string {
213213
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
214-
let finalHint = hint;
215-
216-
// If there's no explicit hint provided, mimic the same thing that would happen
217-
// in the minimal itself to create a consistent behavior.
218-
// We don't do this in the client, as it's the lowest level API, and doing this,
219-
// would prevent user from having full control over direct calls.
220-
if (!hint) {
221-
let syntheticException: Error;
222-
try {
223-
throw new Error(message);
224-
} catch (exception) {
225-
syntheticException = exception as Error;
226-
}
227-
finalHint = {
228-
originalException: message,
229-
syntheticException,
230-
};
214+
215+
let syntheticException: Error;
216+
try {
217+
throw new Error('Sentry syntheticException');
218+
} catch (exception) {
219+
syntheticException = exception as Error;
231220
}
232221

233222
this._invokeClient('captureMessage', message, level, {
234-
...finalHint,
223+
originalException: message,
224+
syntheticException,
225+
...hint,
235226
event_id: eventId,
236227
});
237228
return eventId;

packages/minimal/src/index.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +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 a message event and sends it to Sentry.
34-
*
35-
* @param message The message to send to Sentry.
36-
* @param Severity Define the level of the message.
37-
* @returns The generated eventId.
38-
*/
39-
export function captureMessage(
40-
message: string,
41-
// eslint-disable-next-line deprecation/deprecation
42-
captureContext?: CaptureContext | Severity | SeverityLevel,
43-
): string {
44-
const syntheticException = new Error(message);
45-
46-
// This is necessary to provide explicit scopes upgrade, without changing the original
47-
// arity of the `captureMessage(message, level)` method.
48-
const level = typeof captureContext === 'string' ? captureContext : undefined;
49-
const context = typeof captureContext !== 'string' ? { captureContext } : undefined;
50-
51-
return callOnHub('captureMessage', message, level, {
52-
originalException: message,
53-
syntheticException,
54-
...context,
55-
});
56-
}
57-
5832
/**
5933
* Captures a manually created event and sends it to Sentry.
6034
*

0 commit comments

Comments
 (0)