Merge pull request #13 from makermelissa/main

Improve error handling
This commit is contained in:
Melissa LeBlanc-Williams 2024-08-15 13:56:01 -07:00 committed by GitHub
commit f8e9b6fd01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

16
repl.js
View file

@ -61,6 +61,7 @@ export class FileOps {
let error = this._repl.getErrorOutput(); let error = this._repl.getErrorOutput();
if (error) { if (error) {
console.error("Python Error - " + error.type + ": " + error.message); console.error("Python Error - " + error.type + ": " + error.message);
this._repl.writeErrorToTerminal(error.raw);
if (error.type == "OSError" && error.errno == 30) { if (error.type == "OSError" && error.errno == 30) {
this._isReadOnly = true; this._isReadOnly = true;
// Throw an error if needed // Throw an error if needed
@ -228,7 +229,7 @@ for item in contents:
try: try:
import storage import storage
print(storage.getmount("/").readonly) print(storage.getmount("/").readonly)
except: except ImportError:
print(False) print(False)
`; `;
let result = await this._repl.runCode(code); let result = await this._repl.runCode(code);
@ -411,6 +412,10 @@ export class REPL {
} }
} }
writeErrorToTerminal(data) {
this.writeToTerminal(`\x1b[91m${data}\x1b[0m`);
}
_sleep(ms) { _sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
@ -457,7 +462,6 @@ export class REPL {
let lastRawPosition = this._findLastRegexPosition(rawModRegex, buffer); let lastRawPosition = this._findLastRegexPosition(rawModRegex, buffer);
let lastNormalPosition = this._findLastRegexPosition(normalModRegex, buffer); let lastNormalPosition = this._findLastRegexPosition(normalModRegex, buffer);
let lastPrePromptPosition = this._findLastRegexPosition(prePromptRegex, buffer); let lastPrePromptPosition = this._findLastRegexPosition(prePromptRegex, buffer);
if (lastPrePromptPosition > lastNormalPosition && lastPrePromptPosition > lastRawPosition) { if (lastPrePromptPosition > lastNormalPosition && lastPrePromptPosition > lastRawPosition) {
this._mode = MODE_PRE_PROMPT; this._mode = MODE_PRE_PROMPT;
if (DEBUG) { if (DEBUG) {
@ -533,7 +537,7 @@ export class REPL {
if (this._rawByteCount >= 2) { if (this._rawByteCount >= 2) {
while (bytes.length > 0) { while (bytes.length > 0) {
if (this._checkpointCount == 0) { if (this._checkpointCount == 0) {
if (bytes.slice(0, 2).match("OK")) { if (bytes.slice(0, 2).match("OK") || bytes.slice(0, 3).match(">OK")) {
this._checkpointCount++; this._checkpointCount++;
bytes = bytes.slice(2); bytes = bytes.slice(2);
} else if (bytes.slice(0, 2).match("ra")) { } else if (bytes.slice(0, 2).match("ra")) {
@ -662,7 +666,7 @@ export class REPL {
await this.serialTransmit(keySequence); await this.serialTransmit(keySequence);
} }
await this._detectCurrentMode(); await this._detectCurrentMode();
await this._sleep(100); await this._sleep(250);
} }
}, 3000 }, 3000
); );
@ -774,8 +778,8 @@ export class REPL {
} }
// Allows for supplied python code to be run on the device via the REPL in normal mode // Allows for supplied python code to be run on the device via the REPL in normal mode
async runCode(code, codeTimeoutMs=CODE_EXECUTION_TIMEOUT) { async runCode(code, codeTimeoutMs=CODE_EXECUTION_TIMEOUT, showOutput=false) {
this.terminalOutput = DEBUG; this.terminalOutput = DEBUG || showOutput;
await this.getToPrompt(); await this.getToPrompt();
let result = await this.execRawMode(code + LINE_ENDING_LF, codeTimeoutMs); let result = await this.execRawMode(code + LINE_ENDING_LF, codeTimeoutMs);