refactor(Compiler): improve the error message on component load error

by adding the fetched url.

relates to #1460
This commit is contained in:
Victor Berchet 2015-06-01 11:07:14 +02:00
parent a504fa835e
commit c60091b949
4 changed files with 15 additions and 9 deletions

View File

@ -36,8 +36,9 @@ export class DomCompiler extends RenderCompiler {
var tplPromise = this._templateLoader.load(template);
return PromiseWrapper.then(
tplPromise, (el) => this._compileTemplate(template, el, ProtoViewDto.COMPONENT_VIEW_TYPE),
(_) => {
throw new BaseException(`Failed to load the template "${template.componentId}"`);
(e) => {
throw new BaseException(
`Failed to load the template for "${template.componentId}" : ${e}`);
});
}

View File

@ -32,10 +32,14 @@ export class TemplateLoader {
var promise = StringMapWrapper.get(this._htmlCache, url);
if (isBlank(promise)) {
promise = this._xhr.get(url).then(function(html) {
// TODO(vicb): change error when TS gets fixed
// https://github.com/angular/angular/issues/2280
// throw new BaseException(`Failed to fetch url "${url}"`);
promise = PromiseWrapper.then(this._xhr.get(url), html => {
var template = DOM.createTemplate(html);
return template;
});
}, _ => PromiseWrapper.reject(new BaseException(`Failed to fetch url "${url}"`), null));
StringMapWrapper.set(this._htmlCache, url, promise);
}

View File

@ -103,7 +103,8 @@ export function runCompilerCommonTests() {
PromiseWrapper.catchError(
compiler.compile(new ViewDefinition({componentId: 'someId', absUrl: 'someUrl'})),
(e) => {
expect(e.message).toContain(`Failed to load the template "someId"`);
expect(e.message).toEqual(
'Failed to load the template for "someId" : Failed to fetch url "someUrl"');
async.done();
return null;
});
@ -208,9 +209,9 @@ class FakeTemplateLoader extends TemplateLoader {
if (isPresent(template.absUrl)) {
var content = MapWrapper.get(this._urlData, template.absUrl);
if (isPresent(content)) {
return PromiseWrapper.resolve(DOM.createTemplate(content));
}
return isPresent(content) ?
PromiseWrapper.resolve(DOM.createTemplate(content)) :
PromiseWrapper.reject(`Failed to fetch url "${template.absUrl}"`, null);
}
return PromiseWrapper.reject('Load failed', null);

View File

@ -78,7 +78,7 @@ export function main() {
var template = new ViewDefinition({absUrl: 'base/foo'});
PromiseWrapper.then(loader.load(template), function(_) { throw 'Unexpected response'; },
function(error) {
expect(error).toEqual('Failed to load base/foo');
expect(error.message).toEqual('Failed to fetch url "base/foo"');
async.done();
});
xhr.flush();