Skip to content

Commit f852b55

Browse files
committed
add throwByDefault flag and require it on every handleRecoverableError call
1 parent 844ffbd commit f852b55

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

packages/bundler-plugin-core/src/debug-id-upload.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { stripQueryAndHashFromPath } from "./utils";
1212
import { setMeasurement, spanToTraceHeader, startSpan } from "@sentry/core";
1313
import { getDynamicSamplingContextFromSpan, Scope } from "@sentry/core";
1414
import { Client } from "@sentry/types";
15+
import { HandleRecoverableErrorFn } from "./types";
1516

1617
interface RewriteSourcesHook {
1718
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -25,7 +26,7 @@ interface DebugIdUploadPluginOptions {
2526
releaseName?: string;
2627
dist?: string;
2728
rewriteSourcesHook?: RewriteSourcesHook;
28-
handleRecoverableError: (error: unknown) => void;
29+
handleRecoverableError: HandleRecoverableErrorFn;
2930
sentryScope: Scope;
3031
sentryClient: Client;
3132
sentryCliOptions: {
@@ -187,7 +188,7 @@ export function createDebugIdUploadFunction({
187188
}
188189
} catch (e) {
189190
sentryScope.captureException('Error in "debugIdUploadPlugin" writeBundle hook');
190-
handleRecoverableError(e);
191+
handleRecoverableError(e, false);
191192
} finally {
192193
if (folderToCleanUp) {
193194
void startSpan({ name: "cleanup", scope: sentryScope }, async () => {

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@ export function sentryUnpluginFactory({
155155
"SENTRY_PIPELINE"
156156
] = `${unpluginMetaContext.framework}-plugin/${__PACKAGE_VERSION__}`;
157157

158-
function handleRecoverableError(unknownError: unknown) {
158+
/**
159+
* Handles errors caught and emitted in various areas of the plugin.
160+
*
161+
* Also sets the sentry session status according to the error handling.
162+
*
163+
* If users specify their custom `errorHandler` we'll leave the decision to throw
164+
* or continue up to them. By default, @param throwByDefault controls if the plugin
165+
* should throw an error (which causes a build fail in most bundlers) or continue.
166+
*/
167+
function handleRecoverableError(unknownError: unknown, throwByDefault: boolean) {
159168
sentrySession.status = "abnormal";
160169
try {
161170
if (options.errorHandler) {
@@ -173,6 +182,9 @@ export function sentryUnpluginFactory({
173182
// setting the session to "crashed" b/c from a plugin perspective this run failed.
174183
// However, we're intentionally not rethrowing the error to avoid breaking the user build.
175184
sentrySession.status = "crashed";
185+
if (throwByDefault) {
186+
throw unknownError;
187+
}
176188
logger.error("An error occurred. Couldn't finish all operations:", unknownError);
177189
}
178190
} finally {
@@ -181,8 +193,10 @@ export function sentryUnpluginFactory({
181193
}
182194

183195
if (!validateOptions(options, logger)) {
196+
// Throwing by default to avoid a misconfigured plugin going unnoticed.
184197
handleRecoverableError(
185-
new Error("Options were not set correctly. See output above for more details.")
198+
new Error("Options were not set correctly. See output above for more details."),
199+
true
186200
);
187201
}
188202

packages/bundler-plugin-core/src/plugins/release-management.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Scope } from "@sentry/core";
33
import { UnpluginOptions } from "unplugin";
44
import { Logger } from "../sentry/logger";
55
import { safeFlushTelemetry } from "../sentry/telemetry";
6-
import { IncludeEntry } from "../types";
6+
import { HandleRecoverableErrorFn, IncludeEntry } from "../types";
77
import { arrayify } from "../utils";
88
import { Client } from "@sentry/types";
99

@@ -16,7 +16,7 @@ interface ReleaseManagementPluginOptions {
1616
setCommitsOption?: SentryCliCommitsOptions;
1717
deployOptions?: SentryCliNewDeployOptions;
1818
dist?: string;
19-
handleRecoverableError: (error: unknown) => void;
19+
handleRecoverableError: HandleRecoverableErrorFn;
2020
sentryScope: Scope;
2121
sentryClient: Client;
2222
sentryCliOptions: {
@@ -100,7 +100,7 @@ export function releaseManagementPlugin({
100100
} catch (e) {
101101
sentryScope.captureException('Error in "releaseManagementPlugin" writeBundle hook');
102102
await safeFlushTelemetry(sentryClient);
103-
handleRecoverableError(e);
103+
handleRecoverableError(e, false);
104104
} finally {
105105
freeGlobalDependencyOnSourcemapFiles();
106106
freeWriteBundleInvocationDependencyOnSourcemapFiles();

packages/bundler-plugin-core/src/plugins/sourcemap-deletion.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { safeFlushTelemetry } from "../sentry/telemetry";
55
import fs from "fs";
66
import { Scope } from "@sentry/core";
77
import { Client } from "@sentry/types";
8+
import { HandleRecoverableErrorFn } from "../types";
89

910
interface FileDeletionPlugin {
10-
handleRecoverableError: (error: unknown) => void;
11+
handleRecoverableError: HandleRecoverableErrorFn;
1112
waitUntilSourcemapFileDependenciesAreFreed: () => Promise<void>;
1213
sentryScope: Scope;
1314
sentryClient: Client;
@@ -59,7 +60,9 @@ export function fileDeletionPlugin({
5960
} catch (e) {
6061
sentryScope.captureException('Error in "sentry-file-deletion-plugin" buildEnd hook');
6162
await safeFlushTelemetry(sentryClient);
62-
handleRecoverableError(e);
63+
// We throw by default if we get here b/c not being able to delete
64+
// source maps could leak them to production
65+
handleRecoverableError(e, true);
6366
}
6467
},
6568
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,5 @@ type DeployOptions = {
552552
*/
553553
url?: string;
554554
};
555+
556+
export type HandleRecoverableErrorFn = (error: unknown, throwByDefault: boolean) => void;

0 commit comments

Comments
 (0)