Skip to content

Commit f8e9b6f

Browse files
authored
Merge pull request #13 from makermelissa/main
Improve error handling
2 parents 25c8306 + 3f88a13 commit f8e9b6f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

repl.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class FileOps {
6161
let error = this._repl.getErrorOutput();
6262
if (error) {
6363
console.error("Python Error - " + error.type + ": " + error.message);
64+
this._repl.writeErrorToTerminal(error.raw);
6465
if (error.type == "OSError" && error.errno == 30) {
6566
this._isReadOnly = true;
6667
// Throw an error if needed
@@ -228,7 +229,7 @@ for item in contents:
228229
try:
229230
import storage
230231
print(storage.getmount("/").readonly)
231-
except:
232+
except ImportError:
232233
print(False)
233234
`;
234235
let result = await this._repl.runCode(code);
@@ -411,6 +412,10 @@ export class REPL {
411412
}
412413
}
413414

415+
writeErrorToTerminal(data) {
416+
this.writeToTerminal(`\x1b[91m${data}\x1b[0m`);
417+
}
418+
414419
_sleep(ms) {
415420
return new Promise(resolve => setTimeout(resolve, ms));
416421
}
@@ -457,7 +462,6 @@ export class REPL {
457462
let lastRawPosition = this._findLastRegexPosition(rawModRegex, buffer);
458463
let lastNormalPosition = this._findLastRegexPosition(normalModRegex, buffer);
459464
let lastPrePromptPosition = this._findLastRegexPosition(prePromptRegex, buffer);
460-
461465
if (lastPrePromptPosition > lastNormalPosition && lastPrePromptPosition > lastRawPosition) {
462466
this._mode = MODE_PRE_PROMPT;
463467
if (DEBUG) {
@@ -533,7 +537,7 @@ export class REPL {
533537
if (this._rawByteCount >= 2) {
534538
while (bytes.length > 0) {
535539
if (this._checkpointCount == 0) {
536-
if (bytes.slice(0, 2).match("OK")) {
540+
if (bytes.slice(0, 2).match("OK") || bytes.slice(0, 3).match(">OK")) {
537541
this._checkpointCount++;
538542
bytes = bytes.slice(2);
539543
} else if (bytes.slice(0, 2).match("ra")) {
@@ -662,7 +666,7 @@ export class REPL {
662666
await this.serialTransmit(keySequence);
663667
}
664668
await this._detectCurrentMode();
665-
await this._sleep(100);
669+
await this._sleep(250);
666670
}
667671
}, 3000
668672
);
@@ -774,8 +778,8 @@ export class REPL {
774778
}
775779

776780
// Allows for supplied python code to be run on the device via the REPL in normal mode
777-
async runCode(code, codeTimeoutMs=CODE_EXECUTION_TIMEOUT) {
778-
this.terminalOutput = DEBUG;
781+
async runCode(code, codeTimeoutMs=CODE_EXECUTION_TIMEOUT, showOutput=false) {
782+
this.terminalOutput = DEBUG || showOutput;
779783

780784
await this.getToPrompt();
781785
let result = await this.execRawMode(code + LINE_ENDING_LF, codeTimeoutMs);

0 commit comments

Comments
 (0)