Skip to content
Permalink
Browse files

Add code snippet to error message

  • Loading branch information
antonmedv committed Jan 9, 2020
1 parent 973ceb9 commit e5aef9ff32d52730315cd28225ecfa49b7f43611
Showing with 24 additions and 9 deletions.
  1. +24 −9 index.js
@@ -101,15 +101,17 @@ function handle(input) {


function apply(json) {
let output

try {
output = args.reduce(reduce, json)
} catch (e) {
if (e !== std.skip) {
throw e
} else {
return
let output = json

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`
}

0 comments on commit e5aef9f

Please sign in to comment.
You can’t perform that action at this time.