Skip to content

Commit d9a1bde

Browse files
authored
Add support for sentry 'extra' contexts to reportError (#13654)
* Add support for sentry contexts to reportError * use 'extra' context field * use setExtras * consistent naming in sentryLoader for stub * add extras to queue of errors
1 parent 39bce09 commit d9a1bde

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

dotcom-rendering/src/client/sentryLoader/loadSentry.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import type { ReportError } from '../../types/sentry';
66
type ReportErrorError = Parameters<ReportError>[0];
77
type ReportErrorFeature = Parameters<ReportError>[1];
88
type ReportErrorTags = Parameters<ReportError>[2];
9+
type ReportErrorExtras = Parameters<ReportError>[3];
910

1011
type ErrorQueue = Array<{
1112
error: ReportErrorError;
1213
feature: ReportErrorFeature;
1314
tags?: ReportErrorTags;
15+
extras?: ReportErrorExtras;
1416
}>;
1517

1618
const loadSentryCreator = () => {
@@ -29,6 +31,7 @@ const loadSentryCreator = () => {
2931
error: ReportErrorError | undefined,
3032
feature: ReportErrorFeature = 'unknown',
3133
tags?: ReportErrorTags,
34+
extras?: ReportErrorExtras,
3235
) => {
3336
const { endPerformanceMeasure } = startPerformanceMeasure(
3437
'dotcom',
@@ -38,7 +41,7 @@ const loadSentryCreator = () => {
3841
/**
3942
* Queue this error for later
4043
*/
41-
if (error) errorQueue.push({ error, feature, tags });
44+
if (error) errorQueue.push({ error, feature, tags, extras });
4245

4346
/**
4447
* Only initialise once
@@ -87,6 +90,7 @@ const loadSentryCreator = () => {
8790
queuedError.error,
8891
queuedError.feature,
8992
queuedError.tags,
93+
queuedError.extras,
9094
);
9195
}
9296
}
@@ -117,8 +121,13 @@ const loadSentryOnError = (): void => {
117121
window.onunhandledrejection = (
118122
event: undefined | { reason?: unknown },
119123
) => event?.reason instanceof Error && loadSentry(event.reason);
120-
window.guardian.modules.sentry.reportError = (error, feature, tags) => {
121-
loadSentry(error, feature, tags).catch((e) =>
124+
window.guardian.modules.sentry.reportError = (
125+
error,
126+
feature,
127+
tags,
128+
extras,
129+
) => {
130+
loadSentry(error, feature, tags, extras).catch((e) =>
122131
// eslint-disable-next-line no-console -- fallback to console.error
123132
console.error(`loadSentryOnError error: ${String(e)}`),
124133
);

dotcom-rendering/src/client/sentryLoader/sentry.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ if (
5656
Sentry.setTag('dcr.bundle', dcrJavascriptBundle('Variant'));
5757
}
5858

59-
export const reportError: ReportError = (error, feature, tags) => {
59+
export const reportError: ReportError = (error, feature, tags, extras) => {
6060
Sentry.withScope(() => {
6161
Sentry.setTag('feature', feature);
6262
if (tags) {
6363
for (const [key, value] of Object.entries(tags)) {
6464
Sentry.setTag(key, value);
6565
}
6666
}
67+
68+
if (extras) {
69+
Sentry.setExtras(extras);
70+
}
71+
6772
Sentry.captureException(error);
6873
});
6974
};

dotcom-rendering/src/client/sentryLoader/sentryLoader.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ const isSentryEnabled = ({
3434

3535
/** When stubbed errors are only sent to the console */
3636
const stubSentry = (): void => {
37-
window.guardian.modules.sentry.reportError = (error, feature, tags) => {
37+
window.guardian.modules.sentry.reportError = (
38+
error,
39+
feature,
40+
tags,
41+
extras,
42+
) => {
3843
// eslint-disable-next-line no-console -- fallback to console.error
39-
console.error(error, feature, tags);
44+
console.error(error, feature, tags, extras);
4045
};
4146
};
4247

dotcom-rendering/src/types/sentry.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export type ReportError = (
22
error: Error,
33
feature: string,
4-
tags?: {
5-
[key: string]: string;
6-
},
4+
tags?: Record<string, string>,
5+
extras?: Record<string, unknown>,
76
) => void;

0 commit comments

Comments
 (0)