123456789101112131415161718192021222324252627282930313233 |
- '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
- }
|