docs(core): Document invalid multi token (#48267)

Add error page for invalid multi token runtime error

PR Close #48267
This commit is contained in:
Andrew Scott 2022-11-28 14:43:18 -08:00 committed by Jessica Janiuk
parent 279e7d4465
commit ff84c73603
6 changed files with 32 additions and 9 deletions

View file

@ -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

View file

@ -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)

View file

@ -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;

View file

@ -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));
}

View file

@ -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,

View file

@ -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', () => {
@ -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', () => {