Raise errors instead of logging them
This commit is contained in:
parent
49c3278f2f
commit
782c16df7f
44
ESPLoader.js
44
ESPLoader.js
@ -1,3 +1,5 @@
|
||||
import {ESPError, TimeoutError} from "./error.js";
|
||||
|
||||
const MAGIC_TO_CHIP = {
|
||||
[0x00f01d83]: () => import('./targets/esp32.js'),
|
||||
[0x6921506f]: () => import('./targets/esp32c3.js'), // ESP32C3 eco 1+2
|
||||
@ -160,10 +162,10 @@ class ESPLoader {
|
||||
if (op == null || op_ret == op) {
|
||||
return [val, data];
|
||||
} else {
|
||||
throw("invalid response");
|
||||
throw new ESPError("invalid response");
|
||||
}
|
||||
} catch(e) {
|
||||
if (e === "timeout") {
|
||||
if (e instanceof TimeoutError) {
|
||||
throw(e);
|
||||
}
|
||||
}
|
||||
@ -209,7 +211,7 @@ class ESPLoader {
|
||||
return resp;
|
||||
} catch(e) {
|
||||
console.log("Sync err " + e);
|
||||
throw(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@ -240,7 +242,7 @@ class ESPLoader {
|
||||
//var str = new TextDecoder().decode(res);
|
||||
//this.log(str);
|
||||
} catch (e) {
|
||||
if (e === "timeout") {
|
||||
if (e instanceof TimeoutError) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -253,7 +255,7 @@ class ESPLoader {
|
||||
var resp = await this.sync();
|
||||
return "success";
|
||||
} catch(error) {
|
||||
if (error === "timeout") {
|
||||
if (error instanceof TimeoutError) {
|
||||
if (esp32r0_delay) {
|
||||
this.write_char('_');
|
||||
} else {
|
||||
@ -283,8 +285,7 @@ class ESPLoader {
|
||||
}
|
||||
}
|
||||
if (resp !== "success") {
|
||||
this.log("Failed to connect with the device");
|
||||
return "error";
|
||||
throw new ESPError("Failed to connect with the device");
|
||||
}
|
||||
this.write_char('\n');
|
||||
this.write_char('\r');
|
||||
@ -297,7 +298,7 @@ class ESPLoader {
|
||||
if (chip_magic_value in MAGIC_TO_CHIP) {
|
||||
this.chip = (await MAGIC_TO_CHIP[chip_magic_value]()).default;
|
||||
} else {
|
||||
console.log("Unknown chip magic")
|
||||
throw new ESPError(`Unexpected CHIP magic value ${chip_magic_value}. Failed to autodetect chip type.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -510,10 +511,10 @@ class ESPLoader {
|
||||
var SPI_CMD_USR = (1 << 18);
|
||||
var SPI_USR2_COMMAND_LEN_SHIFT = 28;
|
||||
if(read_bits > 32) {
|
||||
throw "Reading more than 32 bits back from a SPI flash operation is unsupported";
|
||||
throw new ESPError("Reading more than 32 bits back from a SPI flash operation is unsupported");
|
||||
}
|
||||
if (data.length > 64) {
|
||||
throw "Writing more than 64 bytes of data with one SPI command is unsupported";
|
||||
throw new ESPError("Writing more than 64 bytes of data with one SPI command is unsupported");
|
||||
}
|
||||
|
||||
var data_bits = data.length * 8;
|
||||
@ -553,7 +554,7 @@ class ESPLoader {
|
||||
}
|
||||
}
|
||||
if (i === 10) {
|
||||
throw "SPI command did not complete in time";
|
||||
throw new ESPError("SPI command did not complete in time");
|
||||
}
|
||||
var stat = await this.read_reg({addr:SPI_W0_REG});
|
||||
await this.write_reg({addr:SPI_USR_REG, value:old_spi_usr});
|
||||
@ -636,14 +637,12 @@ class ESPLoader {
|
||||
this.FLASH_WRITE_SIZE = 0x4000;
|
||||
return this.chip;
|
||||
} else {
|
||||
this.log("Failed to start stub. Unexpected response");
|
||||
return null;
|
||||
throw new ESPError("Failed to start stub. Unexpected response");
|
||||
}
|
||||
}
|
||||
|
||||
change_baud = async() => {
|
||||
this.log("Changing baudrate to " + this.baudrate);
|
||||
console.log("Changing baudrate to " + this.baudrate);
|
||||
let second_arg = this.IS_STUB ? this.transport.baudrate : 0;
|
||||
let pkt = this._appendArray(this._int_to_bytearray(this.baudrate), this._int_to_bytearray(second_arg));
|
||||
let resp = await this.command({op:this.ESP_CHANGE_BAUDRATE, data:pkt});
|
||||
@ -658,11 +657,7 @@ class ESPLoader {
|
||||
}
|
||||
|
||||
main_fn = async ({mode='default_reset'} = {}) => {
|
||||
await this.detect_chip({mode:mode});
|
||||
if (this.chip == null) {
|
||||
this.log("Error in connecting to board");
|
||||
return;
|
||||
}
|
||||
await this.detect_chip();
|
||||
|
||||
var chip = await this.chip.get_chip_description(this);
|
||||
this.log("Chip is " + chip);
|
||||
@ -698,8 +693,7 @@ class ESPLoader {
|
||||
|
||||
parse_flash_size_arg = function(flsz) {
|
||||
if (typeof this.chip.FLASH_SIZES[flsz] === 'undefined') {
|
||||
this.log("Flash size " + flsz + " is not supported by this chip type. Supported sizes: " + this.chip.FLASH_SIZES);
|
||||
throw "Invalid flash size";
|
||||
throw new ESPError("Flash size " + flsz + " is not supported by this chip type. Supported sizes: " + this.chip.FLASH_SIZES);
|
||||
}
|
||||
return this.chip.FLASH_SIZES[flsz];
|
||||
}
|
||||
@ -758,8 +752,7 @@ class ESPLoader {
|
||||
let flash_end = this.flash_size_bytes(flash_size);
|
||||
for (var i = 0; i < fileArray.length; i++) {
|
||||
if ((fileArray[i].data.length + fileArray[i].address) > flash_end) {
|
||||
this.log("Specified file doesn't fit in the available flash");
|
||||
return;
|
||||
throw new ESPError(`File ${i + 1} doesn't fit in the available flash`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -824,7 +817,7 @@ class ESPLoader {
|
||||
timeout = block_timeout;
|
||||
}
|
||||
} else {
|
||||
this.log("Yet to handle Non Compressed writes");
|
||||
throw new ESPError("Yet to handle Non Compressed writes");
|
||||
}
|
||||
bytes_sent += block.length;
|
||||
image = image.slice(this.FLASH_WRITE_SIZE, image.length);
|
||||
@ -842,6 +835,7 @@ class ESPLoader {
|
||||
if (new String(res).valueOf() != new String(calcmd5).valueOf()) {
|
||||
this.log("File md5: " + calcmd5);
|
||||
this.log("Flash md5: " + res);
|
||||
throw new ESPError("MD5 of file does not match data in flash!")
|
||||
} else {
|
||||
this.log("Hash of data verified.");
|
||||
}
|
||||
@ -869,4 +863,4 @@ class ESPLoader {
|
||||
}
|
||||
|
||||
|
||||
export { ESPLoader };
|
||||
export { ESPLoader };
|
||||
|
3
error.js
Normal file
3
error.js
Normal file
@ -0,0 +1,3 @@
|
||||
export class ESPError extends Error {}
|
||||
|
||||
export class TimeoutError extends ESPError {}
|
24
index.js
24
index.js
@ -19,6 +19,7 @@ const alertDiv = document.getElementById('alertDiv');
|
||||
//import { Transport } from './cp210x-webusb.js'
|
||||
import { Transport } from './webserial.js'
|
||||
import { ESPLoader } from './ESPLoader.js'
|
||||
import { ESPError } from './error.js'
|
||||
|
||||
let term = new Terminal({cols:120, rows:40});
|
||||
term.open(terminal);
|
||||
@ -91,8 +92,11 @@ connectButton.onclick = async () => {
|
||||
|
||||
chip = await esploader.main_fn();
|
||||
|
||||
await esploader.flash_id();
|
||||
// Temporarily broken
|
||||
// await esploader.flash_id();
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
term.writeln(`Error: ${e.message}`);
|
||||
}
|
||||
|
||||
console.log("Settings done for :" + chip);
|
||||
@ -121,8 +125,14 @@ resetButton.onclick = async () => {
|
||||
|
||||
eraseButton.onclick = async () => {
|
||||
eraseButton.disabled = true;
|
||||
await esploader.erase_flash();
|
||||
eraseButton.disabled = false;
|
||||
try{
|
||||
await esploader.erase_flash();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
term.writeln(`Error: ${e.message}`);
|
||||
} finally {
|
||||
eraseButton.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
addFile.onclick = async () => {
|
||||
@ -283,6 +293,10 @@ programButton.onclick = async () => {
|
||||
|
||||
fileArr.push({data:fileObj.data, address:offset});
|
||||
}
|
||||
esploader.write_flash({fileArray: fileArr, flash_size: 'keep'});
|
||||
|
||||
try {
|
||||
esploader.write_flash({fileArray: fileArr, flash_size: 'keep'});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
term.writeln(`Error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
'use strict';
|
||||
import {TimeoutError} from './error.js';
|
||||
|
||||
class Transport {
|
||||
constructor(device) {
|
||||
@ -131,7 +131,7 @@ class Transport {
|
||||
|
||||
if (done) {
|
||||
console.log("timed out");
|
||||
throw("timeout");
|
||||
throw new TimeoutError("Timeout");
|
||||
} else {
|
||||
if (timeout > 0) {
|
||||
clearTimeout(t);
|
||||
@ -164,7 +164,7 @@ class Transport {
|
||||
done = o.done;
|
||||
|
||||
if (done) {
|
||||
throw("timeout");
|
||||
throw new TimeoutError("Timeout");
|
||||
} else {
|
||||
if (timeout > 0) {
|
||||
clearTimeout(t);
|
||||
|
Loading…
Reference in New Issue
Block a user