Skip to content

Commit f26f60c

Browse files
OnkarRuikarcaugner
andauthored
enhance(libs/env): improve error message for bad .env configs (#9673)
Co-authored-by: Claas Augner <[email protected]>
1 parent 0807413 commit f26f60c

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Diff for: libs/env/index.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import { VALID_FLAW_CHECKS } from "../constants/index.js";
1010
const dirname = fileURLToPath(new URL(".", import.meta.url));
1111
const ROOT = path.join(dirname, "..", "..");
1212

13+
function parse(value) {
14+
try {
15+
return JSON.parse(value);
16+
} catch (e) {
17+
throw new Error(`Error parsing value '${value}' in .env file: `, {
18+
cause: e,
19+
});
20+
}
21+
}
22+
1323
dotenv.config({
1424
path: path.join(cwd(), process.env.ENV_FILE || ".env"),
1525
});
@@ -31,10 +41,10 @@ export const FOLDERSEARCH = process.env.BUILD_FOLDERSEARCH || "";
3141
export const GOOGLE_ANALYTICS_MEASUREMENT_ID =
3242
process.env.BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID || "";
3343
export const NO_PROGRESSBAR = Boolean(
34-
JSON.parse(process.env.BUILD_NO_PROGRESSBAR || process.env.CI || "false")
44+
parse(process.env.BUILD_NO_PROGRESSBAR || process.env.CI || "false")
3545
);
36-
export const FIX_FLAWS = JSON.parse(process.env.BUILD_FIX_FLAWS || "false");
37-
export const FIX_FLAWS_DRY_RUN = JSON.parse(
46+
export const FIX_FLAWS = parse(process.env.BUILD_FIX_FLAWS || "false");
47+
export const FIX_FLAWS_DRY_RUN = parse(
3848
process.env.BUILD_FIX_FLAWS_DRY_RUN || "false"
3949
);
4050
export const FIX_FLAWS_TYPES = new Set(
@@ -50,14 +60,14 @@ if ([...FIX_FLAWS_TYPES].some((flawType) => !VALID_FLAW_CHECKS.has(flawType))) {
5060
);
5161
}
5262

53-
export const FIX_FLAWS_VERBOSE = JSON.parse(
63+
export const FIX_FLAWS_VERBOSE = parse(
5464
// It's on by default because it's such a sensible option to always have
5565
// on.
5666
process.env.BUILD_FIX_FLAWS_VERBOSE || "true"
5767
);
5868

5969
// See explanation in docs/envvars.md
60-
export const ALWAYS_ALLOW_ROBOTS = JSON.parse(
70+
export const ALWAYS_ALLOW_ROBOTS = parse(
6171
process.env.BUILD_ALWAYS_ALLOW_ROBOTS || "false"
6272
);
6373

@@ -130,7 +140,7 @@ function correctContentPathFromEnv(envVarName) {
130140
// filecheck
131141
// ---------
132142

133-
export const MAX_FILE_SIZE = JSON.parse(
143+
export const MAX_FILE_SIZE = parse(
134144
process.env.FILECHECK_MAX_FILE_SIZE || 500 * 1024 // 500KiB
135145
);
136146

@@ -168,7 +178,7 @@ export const PROXY_HOSTNAME =
168178
export const CONTENT_HOSTNAME = process.env.SERVER_CONTENT_HOST;
169179
export const OFFLINE_CONTENT = process.env.SERVER_OFFLINE_CONTENT === "true";
170180

171-
export const FAKE_V1_API = JSON.parse(process.env.SERVER_FAKE_V1_API || false);
181+
export const FAKE_V1_API = parse(process.env.SERVER_FAKE_V1_API || false);
172182

173183
// ----
174184
// tool

0 commit comments

Comments
 (0)