Skip to content

Commit 0a94c80

Browse files
committed
Normalize template errors
1 parent c70bb2f commit 0a94c80

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/book.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,15 @@ Book.prototype.generate = function(generator) {
235235
return _.reduce(ops["content"] || [], function(prev, file, i) {
236236
return prev.then(function() {
237237
var p = ((i*100)/nFiles).toFixed(0)+"%";
238-
that.log.info.ln("processing", file, p);
239-
return Q(generator.convertFile(file));
238+
that.log.debug.ln("processing", file, p);
239+
240+
return Q(generator.convertFile(file))
241+
.fail(function(err) {
242+
// Transform error message to signal file
243+
throw that.normError(err, {
244+
fileName: file
245+
});
246+
});
240247
});
241248
}, Q());
242249
});
@@ -734,6 +741,32 @@ Book.prototype.i18n = function(phrase) {
734741
return i18n.__.apply({}, [this.config.normalizeLanguage()].concat(args));
735742
};
736743

744+
// Normalize error
745+
Book.prototype.normError = function(err, opts) {
746+
opts = _.defaults(opts || {}, {
747+
748+
});
749+
750+
if (_.isString(err)) err = new Error(err);
751+
752+
// Extend err
753+
_.extend(err, opts);
754+
755+
err.lineNumber = err.lineNumber || err.lineno;
756+
err.columnNumber = err.columnNumber || err.colno;
757+
758+
err.toString = function() {
759+
var attributes = [];
760+
761+
if (this.fileName) attributes.push("In file '"+this.fileName+"'");
762+
if (this.lineNumber) attributes.push("Line "+this.lineNumber);
763+
if (this.columnNumber) attributes.push("Column "+this.columnNumber);
764+
return (this.name || "Error")+": "+this.message+((attributes.length > 0)? " ("+attributes.join(", ")+")" : "")
765+
}
766+
767+
return err;
768+
};
769+
737770
// Init and return a book
738771
Book.init = function(root) {
739772
var book = new Book(root);

lib/template.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,13 @@ TemplateEngine.prototype.renderString = function(content, context, options) {
331331
// Replace shortcuts
332332
content = _.reduce(this.shortcuts, _.partial(this._applyShortcut.bind(this), options.type), content);
333333

334-
return Q.nfcall(this.env.renderString.bind(this.env), content, context, options);
334+
return Q.nfcall(this.env.renderString.bind(this.env), content, context, options)
335+
.fail(function(err) {
336+
if (_.isString(err)) err = new Error(err);
337+
err.message = err.message.replace(/^Error: /, "");
338+
339+
throw err;
340+
});
335341
};
336342

337343
// Render a file from the book

0 commit comments

Comments
 (0)