Skip to content

Commit 92f2060

Browse files
committed
remove throwErrorIfNotFound from getConfig
1 parent 9f80ead commit 92f2060

File tree

2 files changed

+6
-28
lines changed

2 files changed

+6
-28
lines changed

client/utils/getConfig.test.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,10 @@ describe('utils/getConfig()', () => {
4040

4141
// check returns unhappy path
4242
describe('and when the key does not exist in the env file', () => {
43-
it('warns but does not throw if throwErrorIfNotFound is false (default)', () => {
43+
it('warns but does not throw', () => {
4444
expect(() => getConfig('CONFIG_TEST_KEY_NAME')).not.toThrow();
4545
});
4646

47-
it('throws an error if throwErrorIfNotFound is true', () => {
48-
expect(() =>
49-
getConfig('CONFIG_TEST_KEY_NAME', {
50-
throwErrorIfNotFound: true
51-
})
52-
).toThrow();
53-
});
54-
5547
it('returns undefined by default', () => {
5648
const result = getConfig('CONFIG_TEST_KEY_NAME');
5749
expect(result).toBe(undefined);
@@ -70,18 +62,10 @@ describe('utils/getConfig()', () => {
7062
global.process.env.CONFIG_TEST_KEY_NAME = '';
7163
});
7264

73-
it('warns but does not throw if throwErrorIfNotFound is false (default)', () => {
65+
it('warns but does not throw', () => {
7466
expect(() => getConfig('CONFIG_TEST_KEY_NAME')).not.toThrow();
7567
});
7668

77-
it('throws an error if throwErrorIfNotFound is true', () => {
78-
expect(() =>
79-
getConfig('CONFIG_TEST_KEY_NAME', {
80-
throwErrorIfNotFound: true
81-
})
82-
).toThrow();
83-
});
84-
8569
it('returns undefined by default', () => {
8670
const result = getConfig('CONFIG_TEST_KEY_NAME');
8771
expect(result).toBe(undefined);

client/utils/getConfig.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@ export function getEnvVar(key: string): string | undefined {
1414
interface GetConfigOptions {
1515
warn?: boolean;
1616
nullishString?: boolean;
17-
throwErrorIfNotFound?: boolean;
1817
}
1918

2019
const DEFAULT_GET_CONFIG_OPTIONS: GetConfigOptions = {
2120
warn: !isTestEnvironment,
22-
nullishString: false,
23-
throwErrorIfNotFound: false
21+
nullishString: false
2422
};
2523

2624
/**
2725
* Returns a string config value from environment variables.
28-
* Logs a warning or throws an error if the value is missing, if `warn` and `throwErrorIfNotFound` are true in options
26+
* Logs a warning if the value is missing, if `warn` is true in options
2927
*
3028
* @param key - The environment variable key to fetch.
3129
* @param options - Optional settings:
32-
* - `throwErrorIfNotFound`: whether to throw an error if the value is missing (default to `false`).
3330
* - `warn`: whether to warn if the value is missing (default `true` unless in test env).
3431
* - `nullishString`: if true, returns `''` instead of `undefined` when missing.
3532
* @returns String value of the env var, or `''` or `undefined` if missing.
@@ -43,7 +40,7 @@ export function getConfig(
4340
}
4441

4542
// override default options with param options
46-
const { warn, nullishString, throwErrorIfNotFound } = {
43+
const { warn, nullishString } = {
4744
...DEFAULT_GET_CONFIG_OPTIONS,
4845
...options
4946
};
@@ -55,10 +52,7 @@ export function getConfig(
5552
if (value == null || value === '') {
5653
const notFoundMessage = `getConfig("${key}") returned null or undefined`;
5754

58-
// error, warn or continue if no value found:
59-
if (throwErrorIfNotFound && !isTestEnvironment) {
60-
throw new Error(notFoundMessage);
61-
}
55+
// warn or continue if no value found:
6256
if (warn) {
6357
console.warn(notFoundMessage);
6458
}

0 commit comments

Comments
 (0)