forked from tauri-apps/tauri-toml
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-pretty-error.js
39 lines (37 loc) · 1018 Bytes
/
parse-pretty-error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"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;
}