From 6d59f34fc8be458fd0a16de624adc9975370a006 Mon Sep 17 00:00:00 2001 From: harttle Date: Mon, 14 Aug 2017 22:43:49 +0800 Subject: [PATCH] cover all --- src/util/error.js | 46 +++++++++++++++++----------------------------- test/util/error.js | 36 +++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/util/error.js b/src/util/error.js index cdcf85851..3b8cc23cb 100644 --- a/src/util/error.js +++ b/src/util/error.js @@ -1,10 +1,14 @@ const _ = require('./underscore.js') -function TokenizationError (message, token) { +function initError () { + this.name = this.constructor.name if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor) } - this.name = this.constructor.name +} + +function initLiquidError (message, token) { + initError.call(this) this.input = token.input this.line = token.line @@ -12,7 +16,11 @@ function TokenizationError (message, token) { var context = mkContext(token.input, token.line) this.message = mkMessage(message, token) - this.stack = context + '\n' + (this.stack || '') + this.stack = context + '\n' + (this.stack || this.message) +} + +function TokenizationError (message, token) { + initLiquidError.call(this, message, token) } TokenizationError.prototype = Object.create(Error.prototype) TokenizationError.prototype.constructor = TokenizationError @@ -20,15 +28,8 @@ TokenizationError.prototype.constructor = TokenizationError function ParseError (e, token) { _.assign(this, e) this.originalError = e - this.name = this.constructor.name - this.input = token.input - this.line = token.line - this.file = token.file - - var context = mkContext(token.input, token.line) - this.message = mkMessage(e.message || 'Unkown Error', token) - this.stack = context + '\n' + (e.stack || '') + initLiquidError.call(this, e.message, token) } ParseError.prototype = Object.create(Error.prototype) ParseError.prototype.constructor = ParseError @@ -40,35 +41,22 @@ function RenderError (e, tpl) { } _.assign(this, e) this.originalError = e - this.name = this.constructor.name - this.input = tpl.token.input - this.line = tpl.token.line - this.file = tpl.token.file - - var context = mkContext(tpl.token.input, tpl.token.line) - this.message = mkMessage(e.message || 'Unkown Error', tpl.token) - this.stack = context + '\n' + (e.stack || '') + initLiquidError.call(this, e.message, tpl.token) } RenderError.prototype = Object.create(Error.prototype) RenderError.prototype.constructor = RenderError function RenderBreakError (message) { - if (Error.captureStackTrace) { - Error.captureStackTrace(this, this.constructor) - } - this.name = this.constructor.name - this.message = message || '' + initError.call(this) + this.message = message + '' } RenderBreakError.prototype = Object.create(Error.prototype) RenderBreakError.prototype.constructor = RenderBreakError function AssertionError (message) { - if (Error.captureStackTrace) { - Error.captureStackTrace(this, this.constructor) - } - this.name = this.constructor.name - this.message = message + initError.call(this) + this.message = message + '' } AssertionError.prototype = Object.create(Error.prototype) AssertionError.prototype.constructor = AssertionError diff --git a/test/util/error.js b/test/util/error.js index e964e7bfa..c979ab976 100644 --- a/test/util/error.js +++ b/test/util/error.js @@ -1,5 +1,7 @@ const chai = require('chai') const expect = chai.expect +// const error = require('../../src/util/error.js') +// const TokenizationError = error.TokenizationError const mock = require('mock-fs') chai.use(require('chai-as-promised')) @@ -64,6 +66,19 @@ describe('error', function () { expect(err.stack).to.contain('at Object.parse') }) }) + describe('captureStackTrace compatibility', function () { + var captureStackTrace = Error.captureStackTrace + before(() => (Error.captureStackTrace = null)) + after(() => (Error.captureStackTrace = captureStackTrace)) + it('should use empty string if captureStackTrace not defined', function () { + return expect(engine.parseAndRender('{% . a %}')).to.eventually + .be.rejected + .then(function (err) { + expect(err.stack).to.contain('illegal tag syntax') + expect(err.stack).to.not.contain('at Object.parse') + }) + }) + }) it('should contain file path in err.file', function () { var html = '\n\n\n{% . a %}\n\n' mock({ @@ -145,7 +160,7 @@ describe('error', function () { ' 5| 5th', ' 6| 6th', ' 7| 7th', - 'Error: intended render error' + 'RenderError: intended render error' ] return expect(engine.parseAndRender(html.join('\n'))).to.eventually .be.rejected @@ -175,7 +190,7 @@ describe('error', function () { ' 5| 5th', ' 6| {%block%}{%endblock%}', ' 7| 7th', - 'Error: intended render error' + 'RenderError: intended render error' ] return expect(engine.parseAndRender(html)).to.eventually .be.rejected @@ -200,7 +215,7 @@ describe('error', function () { ' 5| 5th', ' 6| 6th', ' 7| 7th', - 'Error: intended render error' + 'RenderError: intended render error' ] return expect(engine.parseAndRender(html)).to.eventually .be.rejected @@ -314,7 +329,7 @@ describe('error', function () { ' 5| 5th', ' 6| 6th', ' 7| 7th', - 'AssertionError: tag a not found' + 'ParseError: tag a not found' ] return expect(engine.parseAndRender(html.join('\n'))).to.eventually .be.rejected @@ -332,7 +347,7 @@ describe('error', function () { '>> 2| X{% a %} {% enda %} Y', ' 3| 3rd', ' 4| 4th', - 'AssertionError: tag a not found' + 'ParseError: tag a not found' ] return expect(engine.parseAndRender(html.join('\n'))).to.eventually .be.rejected @@ -342,15 +357,6 @@ describe('error', function () { }) }) - it('should contain the whole template content in err.input', function () { - var html = 'bar\nfoo{% a %}\nfoo' - return expect(engine.parseAndRender(html)).to.eventually - .be.rejected - .then(function (err) { - expect(err.input).to.equal(html) - }) - }) - it('should contain line number in err.line', function () { var html = '\n\n\n{% raw %}\n\n' return expect(engine.parseAndRender(html)).to.eventually @@ -364,7 +370,7 @@ describe('error', function () { return expect(engine.parseAndRender('{% -a %}')).to.eventually .be.rejected .then(function (err) { - expect(err.stack).to.contain('AssertionError: tag -a not found') + expect(err.stack).to.contain('ParseError: tag -a not found') expect(err.stack).to.match(/at .*:\d+:\d+\)/) }) })