diff --git a/src/util/error.js b/src/util/error.js index ba855cf82..e68526a0b 100644 --- a/src/util/error.js +++ b/src/util/error.js @@ -31,6 +31,10 @@ ParseError.prototype = Object.create(Error.prototype); ParseError.prototype.constructor = ParseError; function RenderError(e, tpl) { + // return the original render error + if(e instanceof RenderError){ + return e; + } this.name = this.constructor.name; this.stack = e.stack; diff --git a/test/util/error.js b/test/util/error.js index cbbf94fad..0a92bf61b 100644 --- a/test/util/error.js +++ b/test/util/error.js @@ -10,6 +10,9 @@ var strictEngine = require('../..')({ }); describe('error', function() { + afterEach(function(){ + mock.restore(); + }); describe('TokenizationError', function() { it('should throw TokenizationError when tag illegal', function() { @@ -78,7 +81,9 @@ describe('error', function() { describe('RenderError', function() { beforeEach(function() { - engine = require('../..')(); + engine = require('../..')({ + root: '/' + }); engine.registerTag('throwingTag', { render: function() { throw new Error('intended render error'); @@ -131,7 +136,7 @@ describe('error', function() { expect(e.message).to.contain('undefined variable: a'); }); }); - it('should contain template content in err.message', function() { + it('should contain template context in err.stack', function() { var html = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']; var message = [ ' 2| 2nd', @@ -150,6 +155,31 @@ describe('error', function() { expect(err.name).to.equal('RenderError'); }); }); + it.only('should contain original template context in err.stack', function() { + var origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']; + mock({ + '/throwing-tag.html': origin.join('\n') + }); + var html = '{%include "throwing-tag.html"%}'; + var message = [ + ' 2| 2nd', + ' 3| 3rd', + '>> 4| X{%throwingTag%} Y', + ' 5| 5th', + ' 6| 6th', + ' 7| 7th', + 'Error: intended render error', + ]; + return expect(engine.parseAndRender(html)).to.eventually + .be.rejected + .then(function(err) { + console.log(err.message); + console.log(err.stack); + expect(err.message).to.equal('intended render error, line:4'); + expect(err.stack).to.contain(message.join('\n')); + expect(err.name).to.equal('RenderError'); + }); + }); it('should contain the whole template content in err.input', function() { var html = 'bar\nfoo{%throwingTag%}\nfoo'; return expect(engine.parseAndRender(html)).to.eventually @@ -245,7 +275,7 @@ describe('error', function() { }); }); - it('should contain template content in err.message', function() { + it('should contain template context in err.stack', function() { var html = ['1st', '2nd', '3rd', 'X{% a %} {% enda %} Y', '5th', '6th', '7th']; var message = [ ' 2| 2nd',