forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_di.ts
More file actions
68 lines (62 loc) · 2.47 KB
/
errors_di.ts
File metadata and controls
68 lines (62 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import type {ProviderToken} from '../di';
import {isEnvironmentProviders} from '../di/interface/provider';
import {RuntimeError, RuntimeErrorCode} from '../errors';
import {Type} from '../interface/type';
import {stringify} from '../util/stringify';
import {stringifyForError} from './util/stringify_utils';
/** Called when directives inject each other (creating a circular dependency) */
export function throwCyclicDependencyError(token: string, path?: string[]): never {
const depPath = path ? `. Dependency path: ${path.join(' > ')} > ${token}` : '';
throw new RuntimeError(
RuntimeErrorCode.CYCLIC_DI_DEPENDENCY,
ngDevMode ? `Circular dependency in DI detected for ${token}${depPath}` : token,
);
}
export function throwMixedMultiProviderError() {
throw new Error(`Cannot mix multi providers and regular providers`);
}
export function throwInvalidProviderError(
ngModuleType?: Type<unknown>,
providers?: any[],
provider?: any,
): never {
if (ngModuleType && providers) {
const providerDetail = providers.map((v) => (v == provider ? '?' + provider + '?' : '...'));
throw new Error(
`Invalid provider for the NgModule '${stringify(
ngModuleType,
)}' - only instances of Provider and Type are allowed, got: [${providerDetail.join(', ')}]`,
);
} else if (isEnvironmentProviders(provider)) {
if (provider.ɵfromNgModule) {
throw new RuntimeError(
RuntimeErrorCode.PROVIDER_IN_WRONG_CONTEXT,
`Invalid providers from 'importProvidersFrom' present in a non-environment injector. 'importProvidersFrom' can't be used for component providers.`,
);
} else {
throw new RuntimeError(
RuntimeErrorCode.PROVIDER_IN_WRONG_CONTEXT,
`Invalid providers present in a non-environment injector. 'EnvironmentProviders' can't be used for component providers.`,
);
}
} else {
throw new Error('Invalid provider');
}
}
/** Throws an error when a token is not found in DI. */
export function throwProviderNotFoundError(
token: ProviderToken<unknown>,
injectorName?: string,
): never {
const errorMessage =
ngDevMode &&
`No provider for ${stringifyForError(token)} found${injectorName ? ` in ${injectorName}` : ''}`;
throw new RuntimeError(RuntimeErrorCode.PROVIDER_NOT_FOUND, errorMessage);
}