mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
feature: rich info error
This commit is contained in:
+66
-25
@@ -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
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user