Skip to content

Commit 84ebd39

Browse files
committed
fix up a few things with sentry setup
1 parent 8167a07 commit 84ebd39

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ interface BundleAnalysisUploadPluginArgs {
1818
unpluginMetaContext: UnpluginContextMeta;
1919
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
2020
sentryClient: SentryClient;
21+
handleRecoverableError: (error: unknown) => void;
2122
}
2223

2324
export const bundleAnalysisPluginFactory = ({
2425
options,
2526
unpluginMetaContext,
2627
bundleAnalysisUploadPlugin,
2728
sentryClient,
29+
handleRecoverableError,
2830
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
2931
const output: Output = {
3032
version: "1",
@@ -101,6 +103,8 @@ export const bundleAnalysisPluginFactory = ({
101103
"none",
102104
{ bundler: unpluginMetaContext.framework },
103105
);
106+
107+
handleRecoverableError(error);
104108
return;
105109
} finally {
106110
sentryClient?.metricsAggregator?.add(
@@ -128,12 +132,13 @@ export const bundleAnalysisPluginFactory = ({
128132
"none",
129133
{ bundler: unpluginMetaContext.framework },
130134
);
131-
} catch {
135+
} catch (error) {
132136
sentryClient?.metricsAggregator?.add(
133137
"c",
134138
"upload_bundle_stats.error",
135139
1,
136140
);
141+
handleRecoverableError(error);
137142
return;
138143
} finally {
139144
sentryClient?.metricsAggregator?.add(

packages/bundler-plugin-core/src/index.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ function codecovUnpluginFactory({
4343

4444
const options = normalizedOptions.options;
4545

46-
const { sentryClient } = createSentryInstance(
47-
options,
48-
unpluginMetaContext.framework,
49-
);
50-
5146
if (!satisfies(process.version, NODE_VERSION_RANGE)) {
5247
red(
5348
`Codecov ${unpluginMetaContext.framework} bundler plugin requires Node.js ${NODE_VERSION_RANGE}. You are using Node.js ${process.version}. Please upgrade your Node.js version.`,
@@ -56,13 +51,56 @@ function codecovUnpluginFactory({
5651
return plugins;
5752
}
5853

54+
const { sentryClient } = createSentryInstance(
55+
options,
56+
unpluginMetaContext.framework,
57+
);
58+
59+
const sentrySession = sentryHub?.startSession();
60+
sentryHub?.captureSession();
61+
62+
let sentEndSession = false; // Just to prevent infinite loops with beforeExit, which is called whenever the event loop empties out
63+
// We also need to manually end sesisons on errors because beforeExit is not called on crashes
64+
process.on("beforeExit", () => {
65+
if (!sentEndSession) {
66+
sentryHub?.endSession();
67+
sentEndSession = true;
68+
}
69+
});
70+
71+
function handleRecoverableError(unknownError: unknown) {
72+
if (sentrySession) {
73+
sentrySession.status = "abnormal";
74+
try {
75+
if (options.errorHandler) {
76+
try {
77+
if (unknownError instanceof Error) {
78+
options.errorHandler(unknownError);
79+
} else {
80+
options.errorHandler(new Error("An unknown error occurred"));
81+
}
82+
} catch (e) {
83+
sentrySession.status = "crashed";
84+
throw e;
85+
}
86+
} else {
87+
sentrySession.status = "crashed";
88+
throw unknownError;
89+
}
90+
} finally {
91+
sentryHub?.endSession();
92+
}
93+
}
94+
}
95+
5996
if (options?.enableBundleAnalysis) {
6097
plugins.push(
6198
bundleAnalysisPluginFactory({
6299
options,
63100
unpluginMetaContext,
64101
bundleAnalysisUploadPlugin,
65102
sentryClient,
103+
handleRecoverableError,
66104
}),
67105
);
68106
}

packages/bundler-plugin-core/src/sentry.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ export const createSentryInstance = (
1919
const telemetry = options.telemetry ?? true;
2020

2121
if (telemetry === false || !!options.dryRun) {
22-
return { sentryClient: undefined };
22+
return { sentryClient: undefined, sentryHub: undefined };
2323
}
2424

2525
const client = new NodeClient({
2626
dsn: "https://942e283ea612c29cc3371c6d27f57e58@o26192.ingest.sentry.io/4506739665207296",
2727

28+
_experiments: {
29+
metricsAggregator: true,
30+
},
31+
2832
tracesSampleRate: 1,
2933
sampleRate: 1,
3034

@@ -71,7 +75,7 @@ export const createSentryInstance = (
7175
// increment the counter for the bundler
7276
client.metricsAggregator?.add("c", `bundler-${bundler}`, 1);
7377

74-
return { sentryClient: client };
78+
return { sentryClient: client, sentryHub: hub };
7579
};
7680

7781
export const setTelemetryDataOnHub = (

packages/bundler-plugin-core/src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ export interface Options {
116116
*/
117117
telemetry?: boolean;
118118

119+
/**
120+
* When an error occurs during release creation or sourcemaps upload, the plugin will call this
121+
* function.
122+
*
123+
* By default, the plugin will simply throw an error, thereby stopping the bundling process. If an
124+
* `errorHandler` callback is provided, compilation will continue, unless an error is thrown in
125+
* the provided callback.
126+
*
127+
* To allow compilation to continue but still emit a warning, set this option to the following:
128+
*
129+
* ```js
130+
* (err) => {
131+
* console.warn(err);
132+
* }
133+
* ```
134+
*/
135+
errorHandler?: (err: Error) => void;
136+
119137
sentry?: {
120138
/**
121139
* Only send bundle stats to sentry (used within sentry bundler plugin).

0 commit comments

Comments
 (0)