Skip to content

Commit

Permalink
docs(core): Document invalid multi token (#48267)
Browse files Browse the repository at this point in the history
Add error page for invalid multi token runtime error

PR Close #48267
  • Loading branch information
atscott authored and thePunderWoman committed Dec 13, 2022
1 parent 0f61672 commit d181b93
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
17 changes: 17 additions & 0 deletions aio/content/errors/NG0209.md
@@ -0,0 +1,17 @@
@name Invalid multi provider
@category runtime
@shortDescription Expected provider to be `multi: true` but did not get an Array

@description

The Angular runtime will throw this error when it injects a token intended to be used with `multi: true` but
a non-Array was found instead. For example, `ENVIRONMENT_INITIALIZER` should be provided
like `{provide: ENVIRONMENT_INITIALIZER, multi: true, useValue: () => {...}}`.

<!-- links -->

<!-- external links -->

<!-- end links -->

@reviewed 2022-11-28
2 changes: 1 addition & 1 deletion goldens/public-api/core/errors.md
Expand Up @@ -59,7 +59,7 @@ export const enum RuntimeErrorCode {
// (undocumented)
INVALID_INJECTION_TOKEN = 204,
// (undocumented)
INVALID_MULTI_PROVIDER = 209,
INVALID_MULTI_PROVIDER = -209,
// (undocumented)
MISSING_GENERATED_DEF = 906,
// (undocumented)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/application_init.ts
Expand Up @@ -102,6 +102,7 @@ export class ApplicationInitStatus {

constructor(@Inject(APP_INITIALIZER) @Optional() private readonly appInits:
ReadonlyArray<() => Observable<unknown>| Promise<unknown>| void>) {
// TODO: Throw RuntimeErrorCode.INVALID_MULTI_PROVIDER if appInits is not an array
this.donePromise = new Promise((res, rej) => {
this.resolve = res;
this.reject = rej;
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/application_ref.ts
Expand Up @@ -1051,8 +1051,16 @@ export class ApplicationRef {
this.tick();
this.components.push(componentRef);
// Get the listeners lazily to prevent DI cycles.
const listeners =
this._injector.get(APP_BOOTSTRAP_LISTENER, []).concat(this._bootstrapListeners);
const listeners = this._injector.get(APP_BOOTSTRAP_LISTENER, []);
if (ngDevMode && !Array.isArray(listeners)) {
throw new RuntimeError(
RuntimeErrorCode.INVALID_MULTI_PROVIDER,
'Unexpected type of the `APP_BOOTSTRAP_LISTENER` token value ' +
`(expected an array, but got ${typeof listeners}). ` +
'Please check that the `APP_BOOTSTRAP_LISTENER` token is configured as a ' +
'`multi: true` provider.');
}
listeners.push(...this._bootstrapListeners);
listeners.forEach((listener) => listener(componentRef));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/errors.ts
Expand Up @@ -32,7 +32,7 @@ export const enum RuntimeErrorCode {
INJECTOR_ALREADY_DESTROYED = 205,
PROVIDER_IN_WRONG_CONTEXT = 207,
MISSING_INJECTION_TOKEN = 208,
INVALID_MULTI_PROVIDER = 209,
INVALID_MULTI_PROVIDER = -209,

// Template Errors
MULTIPLE_COMPONENTS_MATCH = -300,
Expand Down
7 changes: 2 additions & 5 deletions packages/core/test/acceptance/environment_injector_spec.ts
Expand Up @@ -8,6 +8,7 @@

import {Component, ComponentFactoryResolver, createEnvironmentInjector, ENVIRONMENT_INITIALIZER, EnvironmentInjector, inject, InjectFlags, InjectionToken, INJECTOR, Injector, NgModuleRef, ViewContainerRef} from '@angular/core';
import {R3Injector} from '@angular/core/src/di/r3_injector';
import {RuntimeError, RuntimeErrorCode} from '@angular/core/src/errors';
import {TestBed} from '@angular/core/testing';

describe('environment injector', () => {
Expand Down Expand Up @@ -117,11 +118,7 @@ describe('environment injector', () => {
useValue: () => {},
}];
expect(() => createEnvironmentInjector(providers, parentEnvInjector))
.toThrowError(
'NG0209: Unexpected type of the `ENVIRONMENT_INITIALIZER` token value ' +
'(expected an array, but got function). ' +
'Please check that the `ENVIRONMENT_INITIALIZER` token is configured as ' +
'a `multi: true` provider.');
.toThrowMatching((e: RuntimeError) => e.code === RuntimeErrorCode.INVALID_MULTI_PROVIDER);
});

it('should adopt environment-scoped providers', () => {
Expand Down

0 comments on commit d181b93

Please sign in to comment.