Skip to content

Commit 2f29922

Browse files
committed
print multiline error position information
1 parent 6567cb6 commit 2f29922

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

index.html

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@
287287
}
288288
}
289289

290+
function toDigits(i, digits) {
291+
return i.toString().padStart(digits, "0");
292+
}
293+
290294
async function runFile(url, showCode = true) {
291295
const term = window.term;
292296
const response = await fetch(url);
@@ -475,7 +479,10 @@
475479
let unlock = await lock();
476480
term.pause();
477481
// multiline should be split (useful when pasting)
478-
for (const c of command.split("\n")) {
482+
let counter = 0;
483+
const lines = command.split("\n");
484+
for (const c of lines) {
485+
counter++;
479486
const escaped = c.replaceAll(/\u00a0/g, " ");
480487
let fut = pyconsole.push(escaped);
481488
term.set_prompt(fut.syntax_check === "incomplete" ? ps2 : ps1);
@@ -529,7 +536,26 @@
529536
} catch (e) {
530537
if (e.constructor.name === "PythonError") {
531538
const message = fut.formatted_error || e.message;
532-
term.error(message.trimEnd());
539+
let msg = "";
540+
if (lines.length > 1) {
541+
// print the error position in multiline mode
542+
const limit = 3;
543+
const maxDigits = (counter + limit).toString().length;
544+
msg += `Error occred at line ${counter}:\n`;
545+
msg += "----------------------------------------\n"
546+
let i = 0;
547+
for (const line of lines) {
548+
i++;
549+
if (i === counter) {
550+
msg += toDigits(i, maxDigits) + ": " + line + " <-- Error here!" + "\n";
551+
} else if ((i >= counter - limit) && (i <= counter + limit)) {
552+
msg += toDigits(i, maxDigits) + ": " + line + "\n";
553+
}
554+
}
555+
msg += "----------------------------------------\n"
556+
}
557+
msg += message.trimEnd();
558+
term.error(msg);
533559
break;
534560
} else {
535561
throw e;

0 commit comments

Comments
 (0)