Skip to content

Commit 978571a

Browse files
committed
feat(#133): improves error message for $env missing values
1 parent f97b302 commit 978571a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

app-config-extension-utils/src/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ export function forKey(
3737
};
3838
}
3939

40+
export function keysToPath(keys: ParsingExtensionKey[]): string {
41+
if (keys.length === 0) return 'root';
42+
43+
return (
44+
keys
45+
.map(([, k]) => k)
46+
.filter((v) => v)
47+
.join('.') || 'root'
48+
);
49+
}
50+
4051
export class ParsingExtensionInvalidOptions extends AppConfigError {}
4152

4253
export function validateOptions<T>(
@@ -90,13 +101,9 @@ export function validationFunction<T>(
90101
} catch (error) {
91102
const message = error instanceof Error ? error.message : 'unknown';
92103

93-
const parents =
94-
ctx
95-
.map(([, k]) => k)
96-
.filter((v) => !!v)
97-
.join('.') || 'root';
98-
99-
throw new ParsingExtensionInvalidOptions(`Validation failed in "${parents}": ${message}`);
104+
throw new ParsingExtensionInvalidOptions(
105+
`Validation failed in "${keysToPath(ctx)}": ${message}`,
106+
);
100107
}
101108
};
102109
}

app-config-extensions/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import isEqual from 'lodash.isequal';
22
import {
33
forKey,
4+
keysToPath,
45
validateOptions,
56
validationFunction,
67
ValidationFunction,
@@ -173,7 +174,7 @@ export function envDirective(
173174
'$env',
174175
validateOptions(
175176
(SchemaBuilder) => SchemaBuilder.emptySchema().addAdditionalProperties(),
176-
(value) => (parse) => {
177+
(value, _, ctx) => (parse) => {
177178
if (!environment) {
178179
if ('none' in value) {
179180
return parse(value.none, metadata);
@@ -184,7 +185,9 @@ export function envDirective(
184185
}
185186

186187
throw new AppConfigError(
187-
`An $env directive was used, but current environment (eg. NODE_ENV) is undefined`,
188+
`An $env directive was used (in ${keysToPath(
189+
ctx,
190+
)}), but current environment (eg. NODE_ENV) is undefined`,
188191
);
189192
}
190193

@@ -201,7 +204,9 @@ export function envDirective(
201204
const found = Object.keys(value).join(', ');
202205

203206
throw new AppConfigError(
204-
`An $env directive was used, but none matched the current environment (wanted ${environment}, saw [${found}])`,
207+
`An $env directive was used (in ${keysToPath(
208+
ctx,
209+
)}), but none matched the current environment (wanted ${environment}, saw [${found}])`,
205210
);
206211
},
207212
// $env is lazy so that non-applicable envs don't get evaluated

0 commit comments

Comments
 (0)