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 /lib/book.js | |
parent | c70bb2fbd8d2405547bc5a48205fd6cbeb6b32be (diff) | |
download | gitbook-0a94c80c4442d4a9ba5c5384eb854909ffa01f5e.zip gitbook-0a94c80c4442d4a9ba5c5384eb854909ffa01f5e.tar.gz gitbook-0a94c80c4442d4a9ba5c5384eb854909ffa01f5e.tar.bz2 |
Normalize template errors
Diffstat (limited to 'lib/book.js')
-rw-r--r-- | lib/book.js | 37 |
1 files changed, 35 insertions, 2 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); |