Skip to content

Commit 66e4122

Browse files
AndrewKushnirthePunderWoman
authored andcommitted
refactor(core): improve an error message when ENVIRONMENT_INITIALIZER is not a multi provider (angular#46829)
Currently if the `ENVIRONMENT_INITIALIZER` token is not configured with `multi: true` flag, the code fails while trying to iterate over the value. This commit checks whether the `ENVIRONMENT_INITIALIZER` token value type is an array and throws a helpful error message. PR Close angular#46829
1 parent df87f4a commit 66e4122

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

goldens/public-api/core/errors.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export const enum RuntimeErrorCode {
4747
// (undocumented)
4848
INVALID_INJECTION_TOKEN = 204,
4949
// (undocumented)
50+
INVALID_MULTI_PROVIDER = 209,
51+
// (undocumented)
5052
MISSING_GENERATED_DEF = 906,
5153
// (undocumented)
5254
MISSING_INJECTION_CONTEXT = -203,

packages/core/src/di/r3_injector.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ export class R3Injector extends EnvironmentInjector {
272272
const previousInjectImplementation = setInjectImplementation(undefined);
273273
try {
274274
const initializers = this.get(ENVIRONMENT_INITIALIZER.multi, EMPTY_ARRAY, InjectFlags.Self);
275+
if (ngDevMode && !Array.isArray(initializers)) {
276+
throw new RuntimeError(
277+
RuntimeErrorCode.INVALID_MULTI_PROVIDER,
278+
'Unexpected type of the `ENVIRONMENT_INITIALIZER` token value ' +
279+
`(expected an array, but got ${typeof initializers}). ` +
280+
'Please check that the `ENVIRONMENT_INITIALIZER` token is configured as a ' +
281+
'`multi: true` provider.');
282+
}
275283
for (const initializer of initializers) {
276284
initializer();
277285
}

packages/core/src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const enum RuntimeErrorCode {
3232
INJECTOR_ALREADY_DESTROYED = 205,
3333
PROVIDER_IN_WRONG_CONTEXT = 207,
3434
MISSING_INJECTION_TOKEN = 208,
35+
INVALID_MULTI_PROVIDER = 209,
3536

3637
// Template Errors
3738
MULTIPLE_COMPONENTS_MATCH = -300,

packages/core/test/acceptance/environment_injector_spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('environment injector', () => {
9696
expect(cRef.instance.service).toBeInstanceOf(Service);
9797
});
9898

99-
it('should support the ENVIRONMENT_INITIALIZER muli-token', () => {
99+
it('should support the ENVIRONMENT_INITIALIZER multi-token', () => {
100100
let initialized = false;
101101
const parentEnvInjector = TestBed.inject(EnvironmentInjector);
102102
createEnvironmentInjector(
@@ -110,6 +110,20 @@ describe('environment injector', () => {
110110
expect(initialized).toBeTrue();
111111
});
112112

113+
it('should throw when the ENVIRONMENT_INITIALIZER is not a multi-token', () => {
114+
const parentEnvInjector = TestBed.inject(EnvironmentInjector);
115+
const providers = [{
116+
provide: ENVIRONMENT_INITIALIZER,
117+
useValue: () => {},
118+
}];
119+
expect(() => createEnvironmentInjector(providers, parentEnvInjector))
120+
.toThrowError(
121+
'NG0209: Unexpected type of the `ENVIRONMENT_INITIALIZER` token value ' +
122+
'(expected an array, but got function). ' +
123+
'Please check that the `ENVIRONMENT_INITIALIZER` token is configured as ' +
124+
'a `multi: true` provider.');
125+
});
126+
113127
it('should adopt environment-scoped providers', () => {
114128
const parentEnvInjector = TestBed.inject(EnvironmentInjector);
115129
const injector = createEnvironmentInjector([], parentEnvInjector);

0 commit comments

Comments
 (0)