cover all

This commit is contained in:
harttle
2017-08-14 22:43:49 +08:00
parent 72597c52e5
commit 6d59f34fc8
2 changed files with 38 additions and 44 deletions
+17 -29
View File
@@ -1,10 +1,14 @@
const _ = require('./underscore.js') const _ = require('./underscore.js')
function TokenizationError (message, token) { function initError () {
this.name = this.constructor.name
if (Error.captureStackTrace) { if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor) Error.captureStackTrace(this, this.constructor)
} }
this.name = this.constructor.name }
function initLiquidError (message, token) {
initError.call(this)
this.input = token.input this.input = token.input
this.line = token.line this.line = token.line
@@ -12,7 +16,11 @@ function TokenizationError (message, token) {
var context = mkContext(token.input, token.line) var context = mkContext(token.input, token.line)
this.message = mkMessage(message, token) 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 = Object.create(Error.prototype)
TokenizationError.prototype.constructor = TokenizationError TokenizationError.prototype.constructor = TokenizationError
@@ -20,15 +28,8 @@ TokenizationError.prototype.constructor = TokenizationError
function ParseError (e, token) { function ParseError (e, token) {
_.assign(this, e) _.assign(this, e)
this.originalError = e this.originalError = e
this.name = this.constructor.name
this.input = token.input initLiquidError.call(this, e.message, token)
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 || '')
} }
ParseError.prototype = Object.create(Error.prototype) ParseError.prototype = Object.create(Error.prototype)
ParseError.prototype.constructor = ParseError ParseError.prototype.constructor = ParseError
@@ -40,35 +41,22 @@ function RenderError (e, tpl) {
} }
_.assign(this, e) _.assign(this, e)
this.originalError = e this.originalError = e
this.name = this.constructor.name
this.input = tpl.token.input initLiquidError.call(this, e.message, tpl.token)
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 || '')
} }
RenderError.prototype = Object.create(Error.prototype) RenderError.prototype = Object.create(Error.prototype)
RenderError.prototype.constructor = RenderError RenderError.prototype.constructor = RenderError
function RenderBreakError (message) { function RenderBreakError (message) {
if (Error.captureStackTrace) { initError.call(this)
Error.captureStackTrace(this, this.constructor) this.message = message + ''
}
this.name = this.constructor.name
this.message = message || ''
} }
RenderBreakError.prototype = Object.create(Error.prototype) RenderBreakError.prototype = Object.create(Error.prototype)
RenderBreakError.prototype.constructor = RenderBreakError RenderBreakError.prototype.constructor = RenderBreakError
function AssertionError (message) { function AssertionError (message) {
if (Error.captureStackTrace) { initError.call(this)
Error.captureStackTrace(this, this.constructor) this.message = message + ''
}
this.name = this.constructor.name
this.message = message
} }
AssertionError.prototype = Object.create(Error.prototype) AssertionError.prototype = Object.create(Error.prototype)
AssertionError.prototype.constructor = AssertionError AssertionError.prototype.constructor = AssertionError
+21 -15
View File
@@ -1,5 +1,7 @@
const chai = require('chai') const chai = require('chai')
const expect = chai.expect const expect = chai.expect
// const error = require('../../src/util/error.js')
// const TokenizationError = error.TokenizationError
const mock = require('mock-fs') const mock = require('mock-fs')
chai.use(require('chai-as-promised')) chai.use(require('chai-as-promised'))
@@ -64,6 +66,19 @@ describe('error', function () {
expect(err.stack).to.contain('at Object.parse') 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 () { it('should contain file path in err.file', function () {
var html = '<html>\n<head>\n\n{% . a %}\n\n' var html = '<html>\n<head>\n\n{% . a %}\n\n'
mock({ mock({
@@ -145,7 +160,7 @@ describe('error', function () {
' 5| 5th', ' 5| 5th',
' 6| 6th', ' 6| 6th',
' 7| 7th', ' 7| 7th',
'Error: intended render error' 'RenderError: intended render error'
] ]
return expect(engine.parseAndRender(html.join('\n'))).to.eventually return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.be.rejected .be.rejected
@@ -175,7 +190,7 @@ describe('error', function () {
' 5| 5th', ' 5| 5th',
' 6| {%block%}{%endblock%}', ' 6| {%block%}{%endblock%}',
' 7| 7th', ' 7| 7th',
'Error: intended render error' 'RenderError: intended render error'
] ]
return expect(engine.parseAndRender(html)).to.eventually return expect(engine.parseAndRender(html)).to.eventually
.be.rejected .be.rejected
@@ -200,7 +215,7 @@ describe('error', function () {
' 5| 5th', ' 5| 5th',
' 6| 6th', ' 6| 6th',
' 7| 7th', ' 7| 7th',
'Error: intended render error' 'RenderError: intended render error'
] ]
return expect(engine.parseAndRender(html)).to.eventually return expect(engine.parseAndRender(html)).to.eventually
.be.rejected .be.rejected
@@ -314,7 +329,7 @@ describe('error', function () {
' 5| 5th', ' 5| 5th',
' 6| 6th', ' 6| 6th',
' 7| 7th', ' 7| 7th',
'AssertionError: tag a not found' 'ParseError: tag a not found'
] ]
return expect(engine.parseAndRender(html.join('\n'))).to.eventually return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.be.rejected .be.rejected
@@ -332,7 +347,7 @@ describe('error', function () {
'>> 2| X{% a %} {% enda %} Y', '>> 2| X{% a %} {% enda %} Y',
' 3| 3rd', ' 3| 3rd',
' 4| 4th', ' 4| 4th',
'AssertionError: tag a not found' 'ParseError: tag a not found'
] ]
return expect(engine.parseAndRender(html.join('\n'))).to.eventually return expect(engine.parseAndRender(html.join('\n'))).to.eventually
.be.rejected .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 () { it('should contain line number in err.line', function () {
var html = '<html>\n<head>\n\n{% raw %}\n\n' var html = '<html>\n<head>\n\n{% raw %}\n\n'
return expect(engine.parseAndRender(html)).to.eventually return expect(engine.parseAndRender(html)).to.eventually
@@ -364,7 +370,7 @@ describe('error', function () {
return expect(engine.parseAndRender('{% -a %}')).to.eventually return expect(engine.parseAndRender('{% -a %}')).to.eventually
.be.rejected .be.rejected
.then(function (err) { .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+\)/) expect(err.stack).to.match(/at .*:\d+:\d+\)/)
}) })
}) })