refactor(core): make di error messages tree shakable (#46422)

Tree shake error messages from the production build and introduce error codes.

PR Close #46422
This commit is contained in:
Ramesh Thiruchelvam
2022-06-17 07:30:19 +05:30
committed by Dylan Hunn
parent 7558c558e0
commit 07828528be
5 changed files with 17 additions and 11 deletions
@@ -85,24 +85,21 @@ export function ɵɵinject<T>(token: ProviderToken<T>, flags = InjectFlags.Defau
* Throws an error indicating that a factory function could not be generated by the compiler for a
* particular class.
*
* This instruction allows the actual error message to be optimized away when ngDevMode is turned
* off, saving bytes of generated code while still providing a good experience in dev mode.
*
* The name of the class is not mentioned here, but will be in the generated factory function name
* and thus in the stack trace.
*
* @codeGenApi
*/
export function ɵɵinvalidFactoryDep(index: number): never {
const msg = ngDevMode ?
`This constructor is not compatible with Angular Dependency Injection because its dependency at index ${
index} of the parameter list is invalid.
throw new RuntimeError(
RuntimeErrorCode.INVALID_FACTORY_DEPENDENCY,
ngDevMode &&
`This constructor is not compatible with Angular Dependency Injection because its dependency at index ${
index} of the parameter list is invalid.
This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.
Please check that 1) the type for the parameter at index ${
index} is correct and 2) the correct Angular decorators are defined for this class and its ancestors.` :
'invalid';
throw new Error(msg);
index} is correct and 2) the correct Angular decorators are defined for this class and its ancestors.`);
}
/**
+4 -1
View File
@@ -7,6 +7,7 @@
*/
import {R3DependencyMetadataFacade} from '../../compiler/compiler_facade';
import {RuntimeError, RuntimeErrorCode} from '../../errors';
import {Type} from '../../interface/type';
import {ReflectionCapabilities} from '../../reflection/reflection_capabilities';
import {Host, Inject, Optional, Self, SkipSelf} from '../metadata';
@@ -58,7 +59,9 @@ function reflectDependency(dep: any|any[]): R3DependencyMetadataFacade {
meta.token = param.token;
} else if (param instanceof Attribute) {
if (param.attributeName === undefined) {
throw new Error(`Attribute name must be defined.`);
throw new RuntimeError(
RuntimeErrorCode.INVALID_INJECTION_TOKEN,
ngDevMode && `Attribute name must be defined.`);
}
meta.attribute = param.attributeName;
} else {
+4 -1
View File
@@ -6,7 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/
import {RuntimeError, RuntimeErrorCode} from '../errors';
import {stringify} from '../util/stringify';
import {resolveForwardRef} from './forward_ref';
@@ -35,7 +37,8 @@ export class ReflectiveKey {
*/
constructor(public token: Object, public id: number) {
if (!token) {
throw new Error('Token must be defined!');
throw new RuntimeError(
RuntimeErrorCode.MISSING_INJECTION_TOKEN, ngDevMode && 'Token must be defined!');
}
this.displayName = stringify(this.token);
}
+1
View File
@@ -31,6 +31,7 @@ export const enum RuntimeErrorCode {
INVALID_INJECTION_TOKEN = 204,
INJECTOR_ALREADY_DESTROYED = 205,
PROVIDER_IN_WRONG_CONTEXT = 207,
MISSING_INJECTION_TOKEN = 208,
// Template Errors
MULTIPLE_COMPONENTS_MATCH = -300,