Skip to content

Commit c60091b

Browse files
committed
refactor(Compiler): improve the error message on component load error
by adding the fetched url. relates to angular#1460
1 parent a504fa8 commit c60091b

4 files changed

Lines changed: 15 additions & 9 deletions

File tree

modules/angular2/src/render/dom/compiler/compiler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export class DomCompiler extends RenderCompiler {
3636
var tplPromise = this._templateLoader.load(template);
3737
return PromiseWrapper.then(
3838
tplPromise, (el) => this._compileTemplate(template, el, ProtoViewDto.COMPONENT_VIEW_TYPE),
39-
(_) => {
40-
throw new BaseException(`Failed to load the template "${template.componentId}"`);
39+
(e) => {
40+
throw new BaseException(
41+
`Failed to load the template for "${template.componentId}" : ${e}`);
4142
});
4243
}
4344

modules/angular2/src/render/dom/compiler/template_loader.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ export class TemplateLoader {
3232
var promise = StringMapWrapper.get(this._htmlCache, url);
3333

3434
if (isBlank(promise)) {
35-
promise = this._xhr.get(url).then(function(html) {
35+
// TODO(vicb): change error when TS gets fixed
36+
// https://github.com/angular/angular/issues/2280
37+
// throw new BaseException(`Failed to fetch url "${url}"`);
38+
promise = PromiseWrapper.then(this._xhr.get(url), html => {
3639
var template = DOM.createTemplate(html);
3740
return template;
38-
});
41+
}, _ => PromiseWrapper.reject(new BaseException(`Failed to fetch url "${url}"`), null));
42+
3943
StringMapWrapper.set(this._htmlCache, url, promise);
4044
}
4145

modules/angular2/test/render/dom/compiler/compiler_common_tests.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export function runCompilerCommonTests() {
103103
PromiseWrapper.catchError(
104104
compiler.compile(new ViewDefinition({componentId: 'someId', absUrl: 'someUrl'})),
105105
(e) => {
106-
expect(e.message).toContain(`Failed to load the template "someId"`);
106+
expect(e.message).toEqual(
107+
'Failed to load the template for "someId" : Failed to fetch url "someUrl"');
107108
async.done();
108109
return null;
109110
});
@@ -208,9 +209,9 @@ class FakeTemplateLoader extends TemplateLoader {
208209

209210
if (isPresent(template.absUrl)) {
210211
var content = MapWrapper.get(this._urlData, template.absUrl);
211-
if (isPresent(content)) {
212-
return PromiseWrapper.resolve(DOM.createTemplate(content));
213-
}
212+
return isPresent(content) ?
213+
PromiseWrapper.resolve(DOM.createTemplate(content)) :
214+
PromiseWrapper.reject(`Failed to fetch url "${template.absUrl}"`, null);
214215
}
215216

216217
return PromiseWrapper.reject('Load failed', null);

modules/angular2/test/render/dom/compiler/template_loader_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function main() {
7878
var template = new ViewDefinition({absUrl: 'base/foo'});
7979
PromiseWrapper.then(loader.load(template), function(_) { throw 'Unexpected response'; },
8080
function(error) {
81-
expect(error).toEqual('Failed to load base/foo');
81+
expect(error.message).toEqual('Failed to fetch url "base/foo"');
8282
async.done();
8383
});
8484
xhr.flush();

0 commit comments

Comments
 (0)