feature: rich info error

This commit is contained in:
harttle
2016-11-09 01:58:49 +08:00
parent 295eee8134
commit 5c0209ec8b
12 changed files with 473 additions and 146 deletions
+15 -10
View File
@@ -52,16 +52,18 @@ module.exports = function(Tag, Filter) {
function parseToken(token, tokens) {
try {
switch (token.type) {
case 'tag':
return parseTag(token, tokens);
case 'output':
return parseOutput(token.value);
case 'html':
return token;
var tpl = null;
if (token.type === 'tag') {
tpl = parseTag(token, tokens);
} else if (token.type === 'output') {
tpl = parseOutput(token.value);
} else { // token.type === 'html'
tpl = token;
}
tpl.token = token;
return tpl;
} catch (e) {
throw new ParseError(e.message, token.input, token.line, e);
throw new ParseError(e, token);
}
}
@@ -78,7 +80,7 @@ module.exports = function(Tag, Filter) {
str = str.substr(match.index + match[0].length);
var filters = [];
while(match = lexical.filter.exec(str)){
while (match = lexical.filter.exec(str)) {
filters.push([match[0].trim()]);
}
@@ -95,6 +97,9 @@ module.exports = function(Tag, Filter) {
}
return {
parse, parseTag, parseStream, parseOutput
parse,
parseTag,
parseStream,
parseOutput
};
};
+10 -8
View File
@@ -1,9 +1,9 @@
const Syntax = require('./syntax.js');
const Promise = require('any-promise');
const mapSeries = require('./util/promise.js').mapSeries;
const RenderBreak = require('./util/error.js').RenderBreak;
const RenderBreakError = require('./util/error.js').RenderBreakError;
const RenderError = require('./util/error.js').RenderError;
const assert = require('./util/assert.js');
const _ = require('./util/underscore.js');
var render = {
@@ -15,10 +15,11 @@ var render = {
return renderTemplate.call(this, tpl)
.then(partial => html += partial)
.catch(e => {
if(e instanceof RenderBreak){
if(e instanceof RenderBreakError){
e.resolvedHTML = html;
throw e;
}
throw e;
throw new RenderError(e, tpl);
});
}).then(() => html);
@@ -27,7 +28,8 @@ var render = {
return this.renderTag(template, scope)
.then(partial => partial === undefined ? '' : partial);
} else if (template.type === 'output') {
return Promise.resolve(this.evalOutput(template, scope))
return Promise.resolve()
.then(() => this.evalOutput(template, scope))
.then(partial => partial === undefined ? '' : stringify(partial));
} else { // template.type === 'html'
return Promise.resolve(template.value);
@@ -37,10 +39,10 @@ var render = {
renderTag: function(template, scope) {
if (template.name === 'continue') {
return Promise.reject(new RenderBreak('continue'));
return Promise.reject(new RenderBreakError('continue'));
}
if (template.name === 'break') {
return Promise.reject(new RenderBreak('break'));
return Promise.reject(new RenderBreakError('break'));
}
return template.render(scope);
},
@@ -53,7 +55,7 @@ var render = {
if (scope.get('liquid.strict_filters')) {
throw filter.error;
} else {
val = ''
val = '';
return true;
}
}
+22 -5
View File
@@ -1,10 +1,12 @@
const lexical = require('./lexical.js');
const _ = require('./util/underscore.js');
const Promise = require('any-promise');
const Syntax = require('./syntax.js');
const assert = require('./util/assert.js');
function hash(markup, scope) {
var obj = {}, match;
var obj = {},
match;
lexical.hashCapture.lastIndex = 0;
while (match = lexical.hashCapture.exec(markup)) {
var k = match[1],
@@ -20,9 +22,22 @@ module.exports = function() {
var _tagInstance = {
render: function(scope) {
var obj = hash(this.token.args, scope);
return this.tagImpl.render && this.tagImpl.render(scope, obj) || Promise.resolve('');
var impl = this.tagImpl;
if (typeof impl.render !== 'function') {
return Promise.resolve('');
}
return Promise.resolve()
.then(() => typeof impl.render === 'function' ?
impl.render(scope, obj) : '')
.catch(function(e) {
if (_.isError(e)) {
throw e;
}
var msg = `Please reject with an Error in ${impl.render}, got ${e}`;
throw new Error(msg);
});
},
parse: function(token, tokens){
parse: function(token, tokens) {
this.type = 'tag';
this.token = token;
this.name = token.name;
@@ -30,7 +45,7 @@ module.exports = function() {
var tagImpl = tagImpls[this.name];
assert(tagImpl, `tag ${this.name} not found`);
this.tagImpl = Object.create(tagImpl);
if(this.tagImpl.parse){
if (this.tagImpl.parse) {
this.tagImpl.parse(token, tokens);
}
}
@@ -51,6 +66,8 @@ module.exports = function() {
}
return {
construct, register, clear
construct,
register,
clear
};
};
+5 -14
View File
@@ -4,9 +4,9 @@ const _ = require('./util/underscore.js');
const assert = require('../src/util/assert.js');
function parse(html) {
var tokens = [];
assert(_.isString(html), new TokenizationError('illegal input type'));
assert(_.isString(html), 'illegal input type');
var tokens = [];
var syntax = /({%(.*?)%})|({{(.*?)}})/g;
var result, htmlFragment, token;
var lastMatchEnd = 0, lastMatchBegin = -1, parsedLinesCount = 0;
@@ -27,8 +27,7 @@ function parse(html) {
var match = token.value.match(lexical.tagLine);
if (!match) {
throw new TokenizationError(`illegal tag: ${token.raw}`,
token.input, token.line);
throw new TokenizationError(`illegal tag syntax`, token);
}
token.name = match[1];
token.args = match[2];
@@ -36,8 +35,7 @@ function parse(html) {
tokens.push(token);
}
// output
else {
token = factory('output', 3, result);
else { token = factory('output', 3, result);
tokens.push(token);
}
lastMatchEnd = syntax.lastIndex;
@@ -60,17 +58,10 @@ function parse(html) {
raw: match[offset],
value: match[offset + 1].trim(),
line: getLineNum(match),
input: getLineContent(match)
input: html
};
}
function getLineContent(match) {
var idx1 = match.input.lastIndexOf('\n', match.index);
var idx2 = match.input.indexOf('\n', match.index);
if (idx2 === -1) idx2 = match.input.length;
return match.input.slice(idx1 + 1, idx2);
}
function getLineNum(match) {
var lines = match.input.slice(lastMatchBegin + 1, match.index).split('\n');
parsedLinesCount += lines.length - 1;
+66 -25
View File
@@ -1,46 +1,58 @@
function TokenizationError(message, input, line) {
if(Error.captureStackTrace){
const _ = require('./underscore.js');
function TokenizationError(message, token) {
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
this.name = this.constructor.name;
this.message = message;
this.input = input;
this.line = line;
this.input = token.input;
this.line = token.line;
var context = mkContext(token.input, token.line);
this.message = message + '\n' + context;
}
TokenizationError.prototype = Object.create(Error.prototype);
TokenizationError.prototype.constructor = TokenizationError;
function ParseError(message, input, line, e) {
if(Error.captureStackTrace){
Error.captureStackTrace(this, this.constructor);
} else{
this.stack = "";
}
this.stack += (this.stack ? "\nFrom " : "From ") + e.stack;
function ParseError(e, token) {
this.name = this.constructor.name;
this.originalError = e;
this.stack = e.stack;
this.message = message;
this.input = input;
this.line = line;
this.input = token.input;
this.line = token.line;
var context = mkContext(token.input, token.line);
this.message = e.message + '\n' + context;
}
ParseError.prototype = Object.create(Error.prototype);
ParseError.prototype.constructor = ParseError;
function RenderBreak(message){
if(Error.captureStackTrace){
function RenderError(e, tpl) {
this.name = this.constructor.name;
this.stack = e.stack;
this.input = tpl.token.input;
this.line = tpl.token.line;
var context = mkContext(tpl.token.input, tpl.token.line);
this.message = e.message + '\n' + context;
}
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;
this.message = message || '';
}
RenderBreak.prototype = Object.create(Error.prototype);
RenderBreak.prototype.constructor = RenderBreak;
RenderBreakError.prototype = Object.create(Error.prototype);
RenderBreakError.prototype.constructor = RenderBreakError;
function AssertionError(message){
if(Error.captureStackTrace){
function AssertionError(message) {
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
this.name = this.constructor.name;
@@ -49,6 +61,35 @@ function AssertionError(message){
AssertionError.prototype = Object.create(Error.prototype);
AssertionError.prototype.constructor = AssertionError;
function mkContext(input, line) {
var lines = input.split('\n');
var begin = Math.max(line - 2, 1);
var end = Math.min(line + 3, lines.length);
var context = _
.range(begin, end + 1)
.map(l => [
(l === line) ? '>> ' : ' ',
align(l, end),
'| ',
lines[l - 1]
].join(''))
.join('\n');
return context;
}
function align(n, max) {
var length = (max + '').length;
var str = n + '';
var blank = Array(length - str.length).join(' ');
return blank + str;
}
module.exports = {
TokenizationError, ParseError, RenderBreak, AssertionError
TokenizationError,
ParseError,
RenderBreakError,
AssertionError,
RenderError
};
+32
View File
@@ -7,6 +7,13 @@ function isString(value) {
return value instanceof String || typeof value === 'string';
}
function isError(value) {
var signature = Object.prototype.toString.call(value);
// [object XXXError]
return signature.substr(-6, 5) === 'Error' ||
(typeof value.message == 'string' && typeof value.name == 'string');
}
/*
* Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property.
* The iteratee is invoked with three arguments: (value, key, object).
@@ -88,9 +95,34 @@ function isObject(value) {
return value !== null && typeof value === 'object';
}
/*
* A function to create flexibly-numbered lists of integers,
* handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1.
* Returns a list of integers from start (inclusive) to stop (exclusive),
* incremented (or decremented) by step, exclusive.
* Note that ranges that stop before they start are considered to be zero-length instead of
* negative — if you'd like a negative range, use a negative step.
*/
function range(start, stop, step) {
if (arguments.length === 1) {
stop = start;
start = 0;
}
step = step || 1;
var arr = [];
for (var i = start; i < stop; i += step) {
arr.push(i);
}
return arr;
}
exports.isString = isString;
exports.isArray = isArray;
exports.isObject = isObject;
exports.isError = isError;
exports.range = range;
exports.forOwn = forOwn;
exports.assign = assign;