Skip to content

Commit

Permalink
Enhance error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibek Pandey committed Feb 24, 2025
1 parent 1fd93be commit dac66b5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions bin/strata-client/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use argh::FromArgs;
use toml::value::Table;

use crate::errors::ConfigError;
use crate::errors::{ConfigError, InitError};

/// Configs overridable by environment. Mostly for sensitive data.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -64,20 +64,24 @@ pub struct Args {

impl Args {
/// Get strings of overrides gathered from args.
pub fn get_overrides(&self) -> Vec<String> {
pub fn get_overrides(&self) -> Result<Vec<String>, InitError> {
let mut overrides = self.overrides.clone();
overrides.extend_from_slice(&self.get_direct_overrides());
overrides
overrides.extend_from_slice(&self.get_direct_overrides()?);
Ok(overrides)
}

/// Overrides passed directly as args and not as overrides.
fn get_direct_overrides(&self) -> Vec<String> {
fn get_direct_overrides(&self) -> Result<Vec<String>, InitError> {
let mut overrides = Vec::new();
if self.sequencer {
overrides.push("client.is_sequencer=true".to_string());
}
if let Some(datadir) = &self.datadir {
overrides.push(format!("client.datadir={datadir:?}"));
let dd = datadir.to_str().ok_or(anyhow::anyhow!(
"Invalid datadir override path {:?}",
datadir
))?;
overrides.push(format!("client.datadir={dd}"));
}
if let Some(rpc_host) = &self.rpc_host {
overrides.push(format!("client.rpc_host={rpc_host}"));
Expand All @@ -86,7 +90,7 @@ impl Args {
overrides.push(format!("client.rpc_port={rpc_port}"));
}

overrides
Ok(overrides)
}
}

Expand Down Expand Up @@ -116,7 +120,7 @@ pub fn apply_override(
if let Some(t) = table.get_mut(key).and_then(|v| v.as_table_mut()) {
apply_override(rest, value, t)
} else if table.contains_key(key) {
Err(ConfigError::TraversePrimitiveAt(key.to_string()))
Err(ConfigError::TraverseNonTableAt(key.to_string()))
} else {
Err(ConfigError::MissingKey(key.to_string()))
}
Expand Down Expand Up @@ -211,6 +215,7 @@ mod test {

let overrides = args
.get_overrides()
.unwrap()
.into_iter()
.map(|x| parse_override(&x).unwrap());

Expand Down
4 changes: 2 additions & 2 deletions bin/strata-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub enum ConfigError {
MissingKey(String),

/// Tried to traverse into a primitive.
#[error("can't traverse into primitive: {0}")]
TraversePrimitiveAt(String),
#[error("can't traverse into non-table key: {0}")]
TraverseNonTableAt(String),

/// Invalid override string.
#[error("Invalid override: '{0}'")]
Expand Down
4 changes: 2 additions & 2 deletions bin/strata-client/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn get_config(args: Args) -> Result<Config, InitError> {
let mut override_strs = env_args.get_overrides();

// Extend overrides from args.
override_strs.extend_from_slice(&args.get_overrides());
override_strs.extend_from_slice(&args.get_overrides()?);

// Parse overrides.
let overrides = override_strs
Expand All @@ -42,7 +42,7 @@ pub fn get_config(args: Args) -> Result<Config, InitError> {
// Apply overrides to toml table.
let table = config_toml
.as_table_mut()
.ok_or(ConfigError::TraversePrimitiveAt("".to_string()))?;
.ok_or(ConfigError::TraverseNonTableAt("".to_string()))?;

for (path, val) in overrides {
apply_override(&path, val, table)?;
Expand Down

0 comments on commit dac66b5

Please sign in to comment.