Skip to content

Commit 273e4a9

Browse files
fixup! allow WRANGLER_SEND_METRICS to override whether to report Wrangler crashes to Sentry
1 parent b9f6310 commit 273e4a9

File tree

5 files changed

+67
-10
lines changed

5 files changed

+67
-10
lines changed

.changeset/angry-apes-share.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"wrangler": patch
33
---
44

5-
allow WRANGLER_SEND_METRICS to override whether to report Wrangler crashes to Sentry
5+
Allow WRANGLER_SEND_ERROR_REPORTS env var to override whether to report Wrangler crashes to Sentry

packages/wrangler/src/__tests__/sentry.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe("sentry", () => {
123123
expect(sentryRequests?.length).toEqual(0);
124124
});
125125

126-
it("should not hit sentry (or even ask) after reportable error if WRANGLER_SEND_METRICS is explicitly false", async () => {
126+
it("should not hit sentry (or even ask) after reportable error if WRANGLER_SEND_ERROR_REPORTS is explicitly false", async () => {
127127
// Trigger an API error
128128
msw.use(
129129
http.get(
@@ -138,7 +138,7 @@ describe("sentry", () => {
138138
})
139139
);
140140
await expect(
141-
runWrangler("whoami", { WRANGLER_SEND_METRICS: "false" })
141+
runWrangler("whoami", { WRANGLER_SEND_ERROR_REPORTS: "false" })
142142
).rejects.toMatchInlineSnapshot(`[TypeError: Failed to fetch]`);
143143
expect(std.out).toMatchInlineSnapshot(`
144144
"Getting User settings...
@@ -454,7 +454,7 @@ describe("sentry", () => {
454454
`);
455455
});
456456

457-
it("should hit sentry after reportable error (without confirmation) if WRANGLER_SEND_METRICS is explicitly true", async () => {
457+
it("should hit sentry after reportable error (without confirmation) if WRANGLER_SEND_ERROR_REPORTS is explicitly true", async () => {
458458
// Trigger an API error
459459
msw.use(
460460
http.get(
@@ -469,7 +469,7 @@ describe("sentry", () => {
469469
})
470470
);
471471
await expect(
472-
runWrangler("whoami", { WRANGLER_SEND_METRICS: "true" })
472+
runWrangler("whoami", { WRANGLER_SEND_ERROR_REPORTS: "true" })
473473
).rejects.toMatchInlineSnapshot(`[TypeError: Failed to fetch]`);
474474
expect(std.out).toMatchInlineSnapshot(`
475475
"Getting User settings...

packages/wrangler/src/environment-variables/factory.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UserError } from "../errors";
2+
13
type VariableNames =
24
| "CLOUDFLARE_ACCOUNT_ID"
35
| "CLOUDFLARE_API_BASE_URL"
@@ -22,6 +24,7 @@ type VariableNames =
2224
| "WRANGLER_TOKEN_URL"
2325
| "WRANGLER_OUTPUT_FILE_DIRECTORY"
2426
| "WRANGLER_OUTPUT_FILE_PATH"
27+
| "WRANGLER_SEND_ERROR_REPORTS"
2528
| "WRANGLER_CI_MATCH_TAG"
2629
| "WRANGLER_CI_OVERRIDE_NAME"
2730
| "WRANGLER_BUILD_CONDITIONS"
@@ -35,6 +38,49 @@ type DeprecatedNames =
3538
| "CF_EMAIL"
3639
| "CF_API_BASE_URL";
3740

41+
/**
42+
* Create a function used to access a boolean environment variable. It may return undefined if the variable is not set.
43+
*
44+
* This is not memoized to allow us to change the value at runtime, such as in testing.
45+
*
46+
* The environment variable must be either "true" or "false" (after lowercasing), otherwise it will throw an error.
47+
*/
48+
export function getBooleanEnvironmentVariableFactory(options: {
49+
variableName: VariableNames;
50+
}): () => boolean | undefined;
51+
export function getBooleanEnvironmentVariableFactory(options: {
52+
variableName: VariableNames;
53+
defaultValue: boolean | (() => boolean);
54+
}): () => boolean;
55+
export function getBooleanEnvironmentVariableFactory(options: {
56+
variableName: VariableNames;
57+
defaultValue?: boolean | (() => boolean);
58+
}): () => boolean | undefined {
59+
return () => {
60+
if (
61+
!(options.variableName in process.env) ||
62+
process.env[options.variableName] === undefined
63+
) {
64+
return typeof options.defaultValue === "function"
65+
? options.defaultValue()
66+
: options.defaultValue;
67+
}
68+
69+
switch (process.env[options.variableName]?.toLowerCase()) {
70+
case "true":
71+
return true;
72+
case "false":
73+
return false;
74+
default:
75+
throw new UserError(
76+
`Expected ${options.variableName} to be "true" or "false", but got ${JSON.stringify(
77+
process.env[options.variableName]
78+
)}`
79+
);
80+
}
81+
};
82+
}
83+
3884
/**
3985
* Create a function used to access an environment variable.
4086
*

packages/wrangler/src/environment-variables/misc-variables.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import path from "node:path";
22
import { getGlobalWranglerConfigPath } from "../global-wrangler-config-path";
3-
import { getEnvironmentVariableFactory } from "./factory";
3+
import {
4+
getBooleanEnvironmentVariableFactory,
5+
getEnvironmentVariableFactory,
6+
} from "./factory";
47

58
/**
69
* `WRANGLER_C3_COMMAND` can override the command used by `wrangler init` when delegating to C3.
@@ -38,6 +41,14 @@ export const getWranglerSendMetricsFromEnv = getEnvironmentVariableFactory({
3841
variableName: "WRANGLER_SEND_METRICS",
3942
});
4043

44+
/**
45+
* `WRANGLER_SEND_ERROR_REPORTS` can override whether we attempt to send error reports to Sentry.
46+
*/
47+
export const getWranglerSendErrorReportsFromEnv =
48+
getBooleanEnvironmentVariableFactory({
49+
variableName: "WRANGLER_SEND_ERROR_REPORTS",
50+
});
51+
4152
/**
4253
* Set `WRANGLER_API_ENVIRONMENT` environment variable to "staging" to tell Wrangler to hit the staging APIs rather than production.
4354
*/

packages/wrangler/src/sentry/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { rejectedSyncPromise } from "@sentry/utils";
33
import { fetch } from "undici";
44
import { version as wranglerVersion } from "../../package.json";
55
import { confirm } from "../dialogs";
6-
import { getWranglerSendMetricsFromEnv } from "../environment-variables/misc-variables";
6+
import { getWranglerSendErrorReportsFromEnv } from "../environment-variables/misc-variables";
77
import { logger } from "../logger";
88
import type { BaseTransportOptions, TransportRequest } from "@sentry/types";
99
import type { RequestInit } from "undici";
@@ -151,10 +151,10 @@ export function addBreadcrumb(
151151
// consent if not already granted.
152152
export async function captureGlobalException(e: unknown) {
153153
if (typeof SENTRY_DSN !== "undefined") {
154-
const sendMetricsEnvVar = getWranglerSendMetricsFromEnv();
154+
const sendErrorReportsEnvVar = getWranglerSendErrorReportsFromEnv();
155155
sentryReportingAllowed =
156-
sendMetricsEnvVar !== undefined
157-
? sendMetricsEnvVar
156+
sendErrorReportsEnvVar !== undefined
157+
? sendErrorReportsEnvVar
158158
: await confirm(
159159
"Would you like to report this error to Cloudflare? Wrangler's output and the error details will be shared with the Wrangler team to help us diagnose and fix the issue.",
160160
{ fallbackValue: false }

0 commit comments

Comments
 (0)