epiphany/node_modules/@iarna/toml/parse-pretty-error.js
2023-12-09 22:19:03 -08:00

33 lines
1 KiB
JavaScript
Executable file

'use strict'
module.exports = prettyError
function prettyError (err, buf) {
/* istanbul ignore if */
if (err.pos == null || err.line == null) return err
let msg = err.message
msg += ` at row ${err.line + 1}, col ${err.col + 1}, pos ${err.pos}:\n`
/* istanbul ignore else */
if (buf && buf.split) {
const lines = buf.split(/\n/)
const lineNumWidth = String(Math.min(lines.length, err.line + 3)).length
let linePadding = ' '
while (linePadding.length < lineNumWidth) linePadding += ' '
for (let ii = Math.max(0, err.line - 1); ii < Math.min(lines.length, err.line + 2); ++ii) {
let lineNum = String(ii + 1)
if (lineNum.length < lineNumWidth) lineNum = ' ' + lineNum
if (err.line === ii) {
msg += lineNum + '> ' + lines[ii] + '\n'
msg += linePadding + ' '
for (let hh = 0; hh < err.col; ++hh) {
msg += ' '
}
msg += '^\n'
} else {
msg += lineNum + ': ' + lines[ii] + '\n'
}
}
}
err.message = msg + '\n'
return err
}