Skip to content

Commit 57f8ab2

Browse files
crisbetodylhunn
authored andcommitted
fix(core): better error message when directive extends a component (#45658)
We throw an error when a directive is trying to extend a component, but we don't actually say which class is responsible which can be difficult to track down. These changes add the two class names to the error message. PR Close #45658
1 parent b29b95b commit 57f8ab2

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

packages/core/src/render3/features/inherit_definition_feature.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {ComponentDef, ContentQueriesFunction, DirectiveDef, DirectiveDefFeature,
1414
import {TAttributes} from '../interfaces/node';
1515
import {isComponentDef} from '../interfaces/type_checks';
1616
import {mergeHostAttrs} from '../util/attrs_utils';
17+
import {stringifyForError} from '../util/stringify_utils';
1718

1819
export function getSuperType(type: Type<any>): Type<any>&
1920
{ɵcmp?: ComponentDef<any>, ɵdir?: DirectiveDef<any>} {
@@ -41,7 +42,9 @@ export function ɵɵInheritDefinitionFeature(definition: DirectiveDef<any>|Compo
4142
} else {
4243
if (superType.ɵcmp) {
4344
const errorMessage = (typeof ngDevMode === 'undefined' || ngDevMode) ?
44-
'Directives cannot inherit Components' :
45+
`Directives cannot inherit Components. Directive ${
46+
stringifyForError(definition.type)} is attempting to extend component ${
47+
stringifyForError(superType)}` :
4548
'';
4649
throw new RuntimeError(RuntimeErrorCode.INVALID_INHERITANCE, errorMessage);
4750
}

packages/core/test/acceptance/inherit_definition_feature_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ describe('inheritance', () => {
4040

4141
expect(() => {
4242
TestBed.createComponent(App);
43-
}).toThrowError('NG0903: Directives cannot inherit Components');
43+
})
44+
.toThrowError(
45+
'NG0903: Directives cannot inherit Components. Directive MyDirective is attempting to extend component MyComponent');
4446
});
4547

4648
describe('multiple children', () => {

packages/core/test/linker/inheritance_integration_spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('Inheritance logic', () => {
7373
const template = '<div directiveExtendsComponent>Some content</div>';
7474
TestBed.overrideComponent(App, {set: {template}});
7575
expect(() => TestBed.createComponent(App))
76-
.toThrowError('NG0903: Directives cannot inherit Components');
76+
.toThrowError(
77+
'NG0903: Directives cannot inherit Components. Directive DirectiveExtendsComponent is attempting to extend component ComponentA');
7778
});
7879
});

0 commit comments

Comments
 (0)