diff --git a/goldens/public-api/core/errors.md b/goldens/public-api/core/errors.md index ee293c1cda1..c433d0ea21e 100644 --- a/goldens/public-api/core/errors.md +++ b/goldens/public-api/core/errors.md @@ -51,6 +51,8 @@ export const enum RuntimeErrorCode { // (undocumented) MISSING_INJECTION_CONTEXT = -203, // (undocumented) + MISSING_INJECTION_TOKEN = 208, + // (undocumented) MISSING_LOCALE_DATA = 701, // (undocumented) MISSING_ZONEJS = 908, diff --git a/packages/core/src/di/injector_compatibility.ts b/packages/core/src/di/injector_compatibility.ts index 9060022d81b..2783e93b1c2 100644 --- a/packages/core/src/di/injector_compatibility.ts +++ b/packages/core/src/di/injector_compatibility.ts @@ -85,24 +85,21 @@ export function ɵɵinject(token: ProviderToken, 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.`); } /** diff --git a/packages/core/src/di/jit/util.ts b/packages/core/src/di/jit/util.ts index a19b72340cf..5013e8e10a8 100644 --- a/packages/core/src/di/jit/util.ts +++ b/packages/core/src/di/jit/util.ts @@ -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 { diff --git a/packages/core/src/di/reflective_key.ts b/packages/core/src/di/reflective_key.ts index a3323a8cfb9..6ecec9276d3 100644 --- a/packages/core/src/di/reflective_key.ts +++ b/packages/core/src/di/reflective_key.ts @@ -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); } diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index 9184d810797..89fd475d8ff 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -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,