Skip to content

Commit 101a4aa

Browse files
committed
feat(PrivateComponentLoader): Explicit error message when loading a non-component
fixes angular#1062
1 parent 65d7593 commit 101a4aa

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

modules/angular2/src/core/compiler/private_component_loader.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {Compiler} from './compiler';
22
import {ShadowDomStrategy} from './shadow_dom_strategy';
33
import {EventManager} from 'angular2/src/core/events/event_manager';
44
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
5+
import {Component} from 'angular2/src/core/annotations/annotations';
56
import {PrivateComponentLocation} from './private_component_location';
6-
import {Type} from 'angular2/src/facade/lang';
7+
import {Type, stringify, BaseException} from 'angular2/src/facade/lang';
78

89

910
export class PrivateComponentLoader {
@@ -23,6 +24,11 @@ export class PrivateComponentLoader {
2324

2425
load(type:Type, location:PrivateComponentLocation) {
2526
var annotation = this.directiveMetadataReader.read(type).annotation;
27+
28+
if (!(annotation instanceof Component)) {
29+
throw new BaseException(`Could not load '${stringify(type)}' because it is not a component.`);
30+
}
31+
2632
return this.compiler.compile(type).then((componentProtoView) => {
2733
location.createComponent(
2834
type, annotation,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {ddescribe, describe, it, iit, expect, beforeEach} from 'angular2/test_lib';
2+
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
3+
import {PrivateComponentLoader} from 'angular2/src/core/compiler/private_component_loader';
4+
import {Decorator, Viewport} from 'angular2/src/core/annotations/annotations';
5+
6+
@Decorator({selector: 'someDecorator'})
7+
class SomeDecorator {}
8+
9+
@Viewport({selector: 'someViewport'})
10+
class SomeViewport {}
11+
12+
export function main() {
13+
describe("PrivateComponentLoader", () => {
14+
var loader;
15+
16+
beforeEach(() => {
17+
loader = new PrivateComponentLoader(null, null, null, new DirectiveMetadataReader());
18+
});
19+
20+
describe('Load errors', () => {
21+
it('should throw when trying to load a decorator', () => {
22+
expect(() => loader.load(SomeDecorator, null))
23+
.toThrowError("Could not load 'SomeDecorator' because it is not a component.");
24+
});
25+
26+
it('should throw when trying to load a viewport', () => {
27+
expect(() => loader.load(SomeViewport, null))
28+
.toThrowError("Could not load 'SomeViewport' because it is not a component.");
29+
});
30+
});
31+
});
32+
}

0 commit comments

Comments
 (0)