Skip to content

Commit 78f78f5

Browse files
committed
Catch errors in notifiers
1 parent b2037c5 commit 78f78f5

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

packages/node-renderer/src/shared/errorReporter.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import log from './log';
22
import type { TracingContext } from './tracing';
33

4-
export type MessageNotifier = (msg: string, tracingContext?: TracingContext) => void;
5-
export type ErrorNotifier = (err: Error, tracingContext?: TracingContext) => void;
4+
export type Notifier<T> = (msg: T, tracingContext?: TracingContext) => void;
5+
export type MessageNotifier = Notifier<string>;
6+
export type ErrorNotifier = Notifier<Error>;
67

78
const messageNotifiers: MessageNotifier[] = [];
89
const errorNotifiers: ErrorNotifier[] = [];
@@ -24,27 +25,35 @@ export function addErrorNotifier(notifier: ErrorNotifier) {
2425
/**
2526
* Adds a callback to notify an error tracking service on both string error messages and JavaScript {@link Error}s.
2627
*/
27-
export function addNotifier(notifier: (msg: string | Error) => void) {
28+
export function addNotifier(notifier: Notifier<string | Error>) {
2829
messageNotifiers.push(notifier);
2930
errorNotifiers.push(notifier);
3031
}
3132

33+
function notify<T>(msg: T, tracingContext: TracingContext | undefined, notifiers: Notifier<T>[]) {
34+
log.error(`ErrorReporter notification: ${msg}`);
35+
notifiers.forEach((notifier) => {
36+
try {
37+
notifier(msg, tracingContext);
38+
} catch (e) {
39+
log.error(
40+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
41+
`An error tracking notifier failed: ${(e as Error).message ?? e}\nStack:\n${(e as Error).stack}`,
42+
);
43+
}
44+
});
45+
}
46+
3247
/**
3348
* Reports an error message.
3449
*/
3550
export function message(msg: string, tracingContext?: TracingContext) {
36-
log.error(`ErrorReporter notification: ${msg}`);
37-
messageNotifiers.forEach((notifier) => {
38-
notifier(msg, tracingContext);
39-
});
51+
notify(msg, tracingContext, messageNotifiers);
4052
}
4153

4254
/**
4355
* Reports an error.
4456
*/
4557
export function error(err: Error, tracingContext?: TracingContext) {
46-
log.error(`ErrorReporter notification: ${err}`);
47-
errorNotifiers.forEach((notifier) => {
48-
notifier(err, tracingContext);
49-
});
58+
notify(err, tracingContext, errorNotifiers);
5059
}

packages/node-renderer/tests/shared/tracing.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { jest } from '@jest/globals';
22

33
import * as Sentry from '@sentry/node';
44
import sentryTestkit from 'sentry-testkit';
5+
import * as errorReporter from '../../src/shared/errorReporter';
56
import { trace } from '../../src/shared/tracing';
67
import * as tracingIntegration from '../../src/integrations/sentry';
78

@@ -29,7 +30,7 @@ test('should run function and finish span', async () => {
2930
const message = 'test';
3031
await trace(async () => {
3132
savedSpan = Sentry.getActiveSpan();
32-
Sentry.captureMessage(message);
33+
errorReporter.message(message);
3334
await fn();
3435
}, testTransactionContext);
3536
expect(savedSpan).toBeDefined();

0 commit comments

Comments
 (0)