Skip to content

Improve error handling #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class FileOps {
let error = this._repl.getErrorOutput();
if (error) {
console.error("Python Error - " + error.type + ": " + error.message);
this._repl.writeErrorToTerminal(error.raw);
if (error.type == "OSError" && error.errno == 30) {
this._isReadOnly = true;
// Throw an error if needed
Expand Down Expand Up @@ -228,7 +229,7 @@ for item in contents:
try:
import storage
print(storage.getmount("/").readonly)
except:
except ImportError:
print(False)
`;
let result = await this._repl.runCode(code);
Expand Down Expand Up @@ -411,6 +412,10 @@ export class REPL {
}
}

writeErrorToTerminal(data) {
this.writeToTerminal(`\x1b[91m${data}\x1b[0m`);
}

_sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Expand Down Expand Up @@ -457,7 +462,6 @@ export class REPL {
let lastRawPosition = this._findLastRegexPosition(rawModRegex, buffer);
let lastNormalPosition = this._findLastRegexPosition(normalModRegex, buffer);
let lastPrePromptPosition = this._findLastRegexPosition(prePromptRegex, buffer);

if (lastPrePromptPosition > lastNormalPosition && lastPrePromptPosition > lastRawPosition) {
this._mode = MODE_PRE_PROMPT;
if (DEBUG) {
Expand Down Expand Up @@ -533,7 +537,7 @@ export class REPL {
if (this._rawByteCount >= 2) {
while (bytes.length > 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++;
bytes = bytes.slice(2);
} else if (bytes.slice(0, 2).match("ra")) {
Expand Down Expand Up @@ -662,7 +666,7 @@ export class REPL {
await this.serialTransmit(keySequence);
}
await this._detectCurrentMode();
await this._sleep(100);
await this._sleep(250);
}
}, 3000
);
Expand Down Expand Up @@ -774,8 +778,8 @@ export class REPL {
}

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

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