Skip to content

Commit

Permalink
Add code snippet to error message
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jan 9, 2020
1 parent 973ceb9 commit e5aef9f
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions index.js
Expand Up @@ -101,15 +101,17 @@ function handle(input) {




function apply(json) { function apply(json) {
let output let output = json


try { for (let [i, code] of args.entries()) {
output = args.reduce(reduce, json) try {
} catch (e) { output = reduce(output, code)
if (e !== std.skip) { } catch (e) {
throw e if (e === std.skip) {
} else { return
return }
stderr.write(`${snippet(i, code)}\n${e.stack || e}\n`)
process.exit(1)
} }
} }


Expand All @@ -122,3 +124,16 @@ function apply(json) {
console.log(text) 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.