diff --git a/src/app.rs b/src/app.rs index bb23b0c74..97d3eefbc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -104,8 +104,15 @@ impl AppState { let settings = db.get_settings().expect("expected to get settings"); - let mainnet_app_context = - AppContext::new(Network::Dash, db.clone()).expect("expected Dash config for mainnet"); + let mainnet_app_context = match AppContext::new(Network::Dash, db.clone()) { + Some(context) => context, + None => { + eprintln!( + "Error: Failed to create the AppContext. Expected Dash config for mainnet." + ); + std::process::exit(1); + } + }; let testnet_app_context = AppContext::new(Network::Testnet, db.clone()); let mut identities_screen = IdentitiesScreen::new(&mainnet_app_context); diff --git a/src/config.rs b/src/config.rs index 237839004..849818d73 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,14 @@ impl Config { } } +#[derive(Debug, thiserror::Error)] +pub enum ConfigError { + #[error("{0}")] + LoadError(String), + #[error("No valid network configurations found in .env file or environment variables")] + NoValidConfigs, +} + #[derive(Debug, Deserialize, Clone)] pub struct NetworkConfig { /// Hostname of the Dash Platform node to connect to @@ -49,7 +57,7 @@ pub struct NetworkConfig { impl Config { /// Loads the configuration for all networks from environment variables and `.env` file. - pub fn load() -> Self { + pub fn load() -> Result { // Load the .env file if available if let Err(err) = dotenvy::from_path(".env") { tracing::warn!( @@ -83,10 +91,22 @@ impl Config { } }; - Config { + if mainnet_config.is_none() && testnet_config.is_none() { + return Err(ConfigError::NoValidConfigs); + } else if mainnet_config.is_none() { + return Err(ConfigError::LoadError( + "Failed to load mainnet configuration".into(), + )); + } else if testnet_config.is_none() { + tracing::warn!( + "Failed to load testnet configuration, but successfully loaded mainnet config" + ); + } + + Ok(Config { mainnet_config, testnet_config, - } + }) } } diff --git a/src/context.rs b/src/context.rs index 39db279b5..22c0f83c8 100644 --- a/src/context.rs +++ b/src/context.rs @@ -35,7 +35,13 @@ pub struct AppContext { impl AppContext { pub fn new(network: Network, db: Arc) -> Option> { - let config = Config::load(); + let config = match Config::load() { + Ok(config) => config, + Err(e) => { + println!("Failed to load config: {e}"); + return None; + } + }; let network_config = config.config_for_network(network).clone()?; diff --git a/src/ui/network_chooser_screen.rs b/src/ui/network_chooser_screen.rs index df34659c4..7f063a790 100644 --- a/src/ui/network_chooser_screen.rs +++ b/src/ui/network_chooser_screen.rs @@ -116,6 +116,11 @@ impl NetworkChooserScreen { // Display status indicator ui.colored_label(status_color, if is_working { "Online" } else { "Offline" }); + if network == Network::Testnet && self.testnet_app_context.is_none() { + ui.label("(No configs for testnet loaded)"); + return AppAction::None; + } + // Display wallet count let wallet_count = format!( "{}",