Skip to content

Commit 8413c38

Browse files
committed
fix up a few things with sentry setup
1 parent c907976 commit 8413c38

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
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
@@ -17,13 +17,15 @@ interface BundleAnalysisUploadPluginArgs {
1717
unpluginMetaContext: UnpluginContextMeta;
1818
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
1919
sentryClient: SentryClient;
20+
handleRecoverableError: (error: unknown) => void;
2021
}
2122

2223
export const bundleAnalysisPluginFactory = ({
2324
userOptions,
2425
unpluginMetaContext,
2526
bundleAnalysisUploadPlugin,
2627
sentryClient,
28+
handleRecoverableError,
2729
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
2830
const output: Output = {
2931
version: "1",
@@ -90,6 +92,8 @@ export const bundleAnalysisPluginFactory = ({
9092
"none",
9193
{ bundler: unpluginMetaContext.framework },
9294
);
95+
96+
handleRecoverableError(error);
9397
return;
9498
} finally {
9599
sentryClient?.metricsAggregator?.add(
@@ -116,12 +120,13 @@ export const bundleAnalysisPluginFactory = ({
116120
"none",
117121
{ bundler: unpluginMetaContext.framework },
118122
);
119-
} catch {
123+
} catch (error) {
120124
sentryClient?.metricsAggregator?.add(
121125
"c",
122126
"upload_bundle_stats.error",
123127
1,
124128
);
129+
handleRecoverableError(error);
125130
return;
126131
} finally {
127132
sentryClient?.metricsAggregator?.add(

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,56 @@ function codecovUnpluginFactory({
3232
return createUnplugin<Options, true>((userOptions, unpluginMetaContext) => {
3333
const plugins: UnpluginOptions[] = [];
3434

35+
if (!satisfies(process.version, NODE_VERSION_RANGE)) {
36+
red(
37+
`Codecov ${unpluginMetaContext.framework} bundler plugin requires Node.js ${NODE_VERSION_RANGE}. You are using Node.js ${process.version}. Please upgrade your Node.js version.`,
38+
);
39+
40+
return plugins;
41+
}
42+
3543
const options = normalizeOptions(userOptions);
3644

37-
const { sentryClient } = createSentryInstance(
45+
const { sentryClient, sentryHub } = createSentryInstance(
3846
options,
3947
unpluginMetaContext.framework,
4048
);
4149

42-
if (!satisfies(process.version, NODE_VERSION_RANGE)) {
43-
red(
44-
`Codecov ${unpluginMetaContext.framework} bundler plugin requires Node.js ${NODE_VERSION_RANGE}. You are using Node.js ${process.version}. Please upgrade your Node.js version.`,
45-
);
50+
const sentrySession = sentryHub?.startSession();
51+
sentryHub?.captureSession();
4652

47-
return plugins;
53+
let sentEndSession = false; // Just to prevent infinite loops with beforeExit, which is called whenever the event loop empties out
54+
// We also need to manually end sesisons on errors because beforeExit is not called on crashes
55+
process.on("beforeExit", () => {
56+
if (!sentEndSession) {
57+
sentryHub?.endSession();
58+
sentEndSession = true;
59+
}
60+
});
61+
62+
function handleRecoverableError(unknownError: unknown) {
63+
if (sentrySession) {
64+
sentrySession.status = "abnormal";
65+
try {
66+
if (options.errorHandler) {
67+
try {
68+
if (unknownError instanceof Error) {
69+
options.errorHandler(unknownError);
70+
} else {
71+
options.errorHandler(new Error("An unknown error occurred"));
72+
}
73+
} catch (e) {
74+
sentrySession.status = "crashed";
75+
throw e;
76+
}
77+
} else {
78+
sentrySession.status = "crashed";
79+
throw unknownError;
80+
}
81+
} finally {
82+
sentryHub?.endSession();
83+
}
84+
}
4885
}
4986

5087
if (options?.enableBundleAnalysis) {
@@ -54,6 +91,7 @@ function codecovUnpluginFactory({
5491
unpluginMetaContext,
5592
bundleAnalysisUploadPlugin,
5693
sentryClient,
94+
handleRecoverableError,
5795
}),
5896
);
5997
}

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
@@ -109,6 +109,24 @@ export interface Options {
109109
* Defaults to `true`.
110110
*/
111111
telemetry?: boolean;
112+
113+
/**
114+
* When an error occurs during release creation or sourcemaps upload, the plugin will call this
115+
* function.
116+
*
117+
* By default, the plugin will simply throw an error, thereby stopping the bundling process. If an
118+
* `errorHandler` callback is provided, compilation will continue, unless an error is thrown in
119+
* the provided callback.
120+
*
121+
* To allow compilation to continue but still emit a warning, set this option to the following:
122+
*
123+
* ```js
124+
* (err) => {
125+
* console.warn(err);
126+
* }
127+
* ```
128+
*/
129+
errorHandler?: (err: Error) => void;
112130
}
113131

114132
export type BundleAnalysisUploadPlugin = (

0 commit comments

Comments
 (0)