diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index f129eaac..e639edd7 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -14,13 +14,14 @@ use espflash::{ save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs, }, + error::Error as EspflashError, image_format::ImageFormatKind, logging::initialize_logger, targets::Chip, update::check_for_update, }; use log::{debug, info, LevelFilter}; -use miette::{bail, IntoDiagnostic, Result, WrapErr}; +use miette::{IntoDiagnostic, Result, WrapErr}; use crate::{ cargo_config::CargoConfig, @@ -228,7 +229,7 @@ struct BuildContext { pub fn erase_parts(args: ErasePartsArgs, config: &Config) -> Result<()> { if args.connect_args.no_stub { - bail!("Cannot erase flash without the RAM stub") + return Err(EspflashError::StubRequiredToEraseFlash).into_diagnostic(); } let metadata_partition_table = PackageMetadata::load(&args.package) diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index 14183f73..e95cde63 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -5,21 +5,22 @@ use std::{ }; use clap::{Args, CommandFactory, Parser, Subcommand}; -use espflash::cli::{EraseFlashArgs, EraseRegionArgs}; use espflash::{ cli::{ self, board_info, completions, config::Config, connect, erase_flash, erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_partition_table, parse_uint32, partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs, - ConnectArgs, EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs, + ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs, + MonitorArgs, PartitionTableArgs, }, + error::Error, image_format::ImageFormatKind, logging::initialize_logger, targets::Chip, update::check_for_update, }; use log::{debug, info, LevelFilter}; -use miette::{bail, IntoDiagnostic, Result, WrapErr}; +use miette::{IntoDiagnostic, Result, WrapErr}; #[derive(Debug, Parser)] #[command(about, max_term_width = 100, propagate_version = true, version)] @@ -178,7 +179,7 @@ fn main() -> Result<()> { pub fn erase_parts(args: ErasePartsArgs, config: &Config) -> Result<()> { if args.connect_args.no_stub { - bail!("Cannot erase flash without the RAM stub") + return Err(Error::StubRequiredToEraseFlash).into_diagnostic(); } let mut flash = connect(&args.connect_args, config)?; diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index 95e715d8..6377d4bd 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -21,13 +21,13 @@ use comfy_table::{modifiers, presets::UTF8_FULL, Attribute, Cell, Color, Table}; use esp_idf_part::{DataType, Partition, PartitionTable}; use indicatif::{style::ProgressStyle, HumanCount, ProgressBar}; use log::{debug, info}; -use miette::{bail, IntoDiagnostic, Result, WrapErr}; +use miette::{IntoDiagnostic, Result, WrapErr}; use serialport::{SerialPortType, UsbPortInfo}; use self::{config::Config, monitor::monitor, serial::get_serial_port_info}; use crate::{ elf::ElfFirmwareImage, - error::{MissingPartition, MissingPartitionTable}, + error::{Error, MissingPartition, MissingPartitionTable}, flasher::{FlashFrequency, FlashMode, FlashSize, Flasher, ProgressCallbacks}, image_format::ImageFormatKind, interface::Interface, @@ -472,7 +472,7 @@ impl ProgressCallbacks for EspflashProgress { pub fn erase_flash(args: EraseFlashArgs, config: &Config) -> Result<()> { if args.connect_args.no_stub { - bail!("Cannot erase flash without the RAM stub") + return Err(Error::StubRequiredToEraseFlash).into_diagnostic(); } let mut flash = connect(&args.connect_args, config)?; @@ -487,7 +487,7 @@ pub fn erase_flash(args: EraseFlashArgs, config: &Config) -> Result<()> { pub fn erase_region(args: EraseRegionArgs, config: &Config) -> Result<()> { if args.connect_args.no_stub { - bail!("Cannot erase flash without the RAM stub") + return Err(Error::StubRequiredToEraseFlash).into_diagnostic(); } let mut flash = connect(&args.connect_args, config)?; diff --git a/espflash/src/error.rs b/espflash/src/error.rs index a5169930..2d7a6b18 100644 --- a/espflash/src/error.rs +++ b/espflash/src/error.rs @@ -83,6 +83,13 @@ pub enum Error { )] NoSerial, + #[error("Cannot erase flash without the RAM stub")] + #[diagnostic( + code(espflash::stub_required_to_erase_flash), + help("Don't use the `--no-ram-stub` option with `erase` commands") + )] + StubRequiredToEraseFlash, + #[error("Incorrect serial port configuration")] #[diagnostic( code(espflash::serial_config),