diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-04-02 18:59:56 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-04-02 18:59:56 +0200 |
commit | 0a94c80c4442d4a9ba5c5384eb854909ffa01f5e (patch) | |
tree | e201d8f8d20fdaafa37d644248f4897a7550a6e7 | |
parent | c70bb2fbd8d2405547bc5a48205fd6cbeb6b32be (diff) | |
download | gitbook-0a94c80c4442d4a9ba5c5384eb854909ffa01f5e.zip gitbook-0a94c80c4442d4a9ba5c5384eb854909ffa01f5e.tar.gz gitbook-0a94c80c4442d4a9ba5c5384eb854909ffa01f5e.tar.bz2 |
Normalize template errors
-rw-r--r-- | lib/book.js | 37 | ||||
-rw-r--r-- | lib/template.js | 8 |
2 files changed, 42 insertions, 3 deletions
diff --git a/lib/book.js b/lib/book.js index e357ad8..2f4aa7c 100644 --- a/lib/book.js +++ b/lib/book.js @@ -235,8 +235,15 @@ Book.prototype.generate = function(generator) { return _.reduce(ops["content"] || [], function(prev, file, i) { return prev.then(function() { var p = ((i*100)/nFiles).toFixed(0)+"%"; - that.log.info.ln("processing", file, p); - return Q(generator.convertFile(file)); + that.log.debug.ln("processing", file, p); + + return Q(generator.convertFile(file)) + .fail(function(err) { + // Transform error message to signal file + throw that.normError(err, { + fileName: file + }); + }); }); }, Q()); }); @@ -734,6 +741,32 @@ Book.prototype.i18n = function(phrase) { return i18n.__.apply({}, [this.config.normalizeLanguage()].concat(args)); }; +// Normalize error +Book.prototype.normError = function(err, opts) { + opts = _.defaults(opts || {}, { + + }); + + if (_.isString(err)) err = new Error(err); + + // Extend err + _.extend(err, opts); + + err.lineNumber = err.lineNumber || err.lineno; + err.columnNumber = err.columnNumber || err.colno; + + err.toString = function() { + var attributes = []; + + if (this.fileName) attributes.push("In file '"+this.fileName+"'"); + if (this.lineNumber) attributes.push("Line "+this.lineNumber); + if (this.columnNumber) attributes.push("Column "+this.columnNumber); + return (this.name || "Error")+": "+this.message+((attributes.length > 0)? " ("+attributes.join(", ")+")" : "") + } + + return err; +}; + // Init and return a book Book.init = function(root) { var book = new Book(root); diff --git a/lib/template.js b/lib/template.js index 320c8bf..187df87 100644 --- a/lib/template.js +++ b/lib/template.js @@ -331,7 +331,13 @@ TemplateEngine.prototype.renderString = function(content, context, options) { // Replace shortcuts content = _.reduce(this.shortcuts, _.partial(this._applyShortcut.bind(this), options.type), content); - return Q.nfcall(this.env.renderString.bind(this.env), content, context, options); + return Q.nfcall(this.env.renderString.bind(this.env), content, context, options) + .fail(function(err) { + if (_.isString(err)) err = new Error(err); + err.message = err.message.replace(/^Error: /, ""); + + throw err; + }); }; // Render a file from the book |