Add code snippet to error message

This commit is contained in:
Anton Medvedev 2020-01-09 12:03:01 +03:00
parent 973ceb92da
commit e5aef9ff32

View File

@ -101,15 +101,17 @@ function handle(input) {
function apply(json) {
let output
let output = json
try {
output = args.reduce(reduce, json)
} catch (e) {
if (e !== std.skip) {
throw e
} else {
return
for (let [i, code] of args.entries()) {
try {
output = reduce(output, code)
} catch (e) {
if (e === std.skip) {
return
}
stderr.write(`${snippet(i, code)}\n${e.stack || e}\n`)
process.exit(1)
}
}
@ -122,3 +124,16 @@ function apply(json) {
console.log(text)
}
}
function snippet(i, code) {
let pre = args.slice(0, i).join(' ')
let post = args.slice(i + 1).join(' ')
if (pre.length > 20) {
pre = '...' + pre.substring(pre.length - 20)
}
if (post.length > 20) {
post = post.substring(0, 20) + '...'
}
const chalk = require('chalk')
return `\n ${pre} ${chalk.red.underline(code)} ${post}\n`
}