From 2bc53a38509a4d2f852fd9ee466b18939bcc0724 Mon Sep 17 00:00:00 2001 From: harttle Date: Wed, 16 Nov 2016 00:31:20 +0800 Subject: [PATCH] change: move context error from err.message to err.stack --- src/util/error.js | 9 ++++++--- test/util/error.js | 28 ++++++++++++++++------------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/util/error.js b/src/util/error.js index 7bd734286..ba855cf82 100644 --- a/src/util/error.js +++ b/src/util/error.js @@ -10,7 +10,8 @@ function TokenizationError(message, token) { this.line = token.line; var context = mkContext(token.input, token.line); - this.message = message + '\n' + context; + this.message = message + ', line:' + token.line; + this.stack = context + '\n' + (this.stack || ''); } TokenizationError.prototype = Object.create(Error.prototype); TokenizationError.prototype.constructor = TokenizationError; @@ -23,7 +24,8 @@ function ParseError(e, token) { this.line = token.line; var context = mkContext(token.input, token.line); - this.message = e.message + '\n' + context; + this.message = e.message + ', line:' + token.line; + this.stack = context + '\n' + (this.stack || ''); } ParseError.prototype = Object.create(Error.prototype); ParseError.prototype.constructor = ParseError; @@ -36,7 +38,8 @@ function RenderError(e, tpl) { this.line = tpl.token.line; var context = mkContext(tpl.token.input, tpl.token.line); - this.message = e.message + '\n' + context; + this.message = e.message + ', line:' + tpl.token.line; + this.stack = context + '\n' + (e.stack || ''); } RenderError.prototype = Object.create(Error.prototype); RenderError.prototype.constructor = RenderError; diff --git a/test/util/error.js b/test/util/error.js index 31b368374..cbbf94fad 100644 --- a/test/util/error.js +++ b/test/util/error.js @@ -23,16 +23,17 @@ describe('error', function() { it('should contain template content in err.message', function() { var html = ['1st', '2nd', 'X{% . a %} Y', '4th']; var message = [ - 'illegal tag syntax', ' 1| 1st', ' 2| 2nd', '>> 3| X{% . a %} Y', - ' 4| 4th' + ' 4| 4th', + 'TokenizationError: illegal tag syntax', ]; return expect(engine.parseAndRender(html.join('\n'))).to.eventually .be.rejected .then(function(err) { - expect(err.message).to.equal(message.join('\n')); + expect(err.message).to.equal('illegal tag syntax, line:3'); + expect(err.stack).to.contain(message.join('\n')); expect(err.name).to.equal('TokenizationError'); }); }); @@ -133,18 +134,19 @@ describe('error', function() { it('should contain template content in err.message', function() { var html = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']; var message = [ - 'intended render error', ' 2| 2nd', ' 3| 3rd', '>> 4| X{%throwingTag%} Y', ' 5| 5th', ' 6| 6th', - ' 7| 7th' + ' 7| 7th', + 'Error: intended render error', ]; return expect(engine.parseAndRender(html.join('\n'))).to.eventually .be.rejected .then(function(err) { - expect(err.message).to.equal(message.join('\n')); + expect(err.message).to.equal('intended render error, line:4'); + expect(err.stack).to.contain(message.join('\n')); expect(err.name).to.equal('RenderError'); }); }); @@ -246,18 +248,19 @@ describe('error', function() { it('should contain template content in err.message', function() { var html = ['1st', '2nd', '3rd', 'X{% a %} {% enda %} Y', '5th', '6th', '7th']; var message = [ - 'tag a not found', ' 2| 2nd', ' 3| 3rd', '>> 4| X{% a %} {% enda %} Y', ' 5| 5th', ' 6| 6th', - ' 7| 7th' + ' 7| 7th', + 'AssertionError: tag a not found', ]; return expect(engine.parseAndRender(html.join('\n'))).to.eventually .be.rejected .then(function(err) { - expect(err.message).to.equal(message.join('\n')); + expect(err.message).to.equal('tag a not found, line:4'); + expect(err.stack).to.contain(message.join('\n')); expect(err.name).to.equal('ParseError'); }); }); @@ -265,16 +268,17 @@ describe('error', function() { it('should handle err.message when context not enough', function() { var html = ['1st', 'X{% a %} {% enda %} Y', '3rd', '4th']; var message = [ - 'tag a not found', ' 1| 1st', '>> 2| X{% a %} {% enda %} Y', ' 3| 3rd', - ' 4| 4th' + ' 4| 4th', + 'AssertionError: tag a not found', ]; return expect(engine.parseAndRender(html.join('\n'))).to.eventually .be.rejected .then(function(err) { - expect(err.message).to.equal(message.join('\n')); + expect(err.message).to.equal('tag a not found, line:2'); + expect(err.stack).to.contain(message.join('\n')); }); });