Skip to content

Commit

Permalink
Propagate error if the fs read for ohttp_keys fails during config (#435)
Browse files Browse the repository at this point in the history
Fixes #398
  • Loading branch information
DanGould authored Dec 20, 2024
2 parents e8f1069 + 31bf322 commit b9cb530
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions payjoin-cli/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct AppConfig {

// v2 only
#[cfg(feature = "v2")]
#[serde(deserialize_with = "deserialize_ohttp_keys_from_path")]
pub ohttp_keys: Option<payjoin::OhttpKeys>,
#[cfg(feature = "v2")]
pub ohttp_relay: Url,
Expand Down Expand Up @@ -72,7 +73,8 @@ impl AppConfig {
"ohttp_relay",
matches.get_one::<Url>("ohttp_relay").map(|s| s.as_str()),
)?
.set_default("pj_directory", "https://payjo.in")?;
.set_default("pj_directory", "https://payjo.in")?
.set_default("ohttp_keys", None::<String>)?;

let builder = match matches.subcommand() {
Some(("send", _)) => builder,
Expand Down Expand Up @@ -101,17 +103,7 @@ impl AppConfig {
)?
.set_override_option(
"ohttp_keys",
matches.get_one::<String>("ohttp_keys").and_then(|s| {
std::fs::read(s)
.map_err(|e| {
log::error!("Failed to read ohttp_keys file: {}", e);
ConfigError::Message(format!(
"Failed to read ohttp_keys file: {}",
e
))
})
.ok()
}),
matches.get_one::<String>("ohttp_keys").map(|s| s.as_str()),
)?
};

Expand All @@ -135,3 +127,25 @@ impl AppConfig {
Ok(app_config)
}
}

#[cfg(feature = "v2")]
fn deserialize_ohttp_keys_from_path<'de, D>(
deserializer: D,
) -> Result<Option<payjoin::OhttpKeys>, D::Error>
where
D: serde::Deserializer<'de>,
{
let path_str: Option<String> = Option::deserialize(deserializer)?;

match path_str {
None => Ok(None),
Some(path) => std::fs::read(path)
.map_err(|e| serde::de::Error::custom(format!("Failed to read ohttp_keys file: {}", e)))
.and_then(|bytes| {
payjoin::OhttpKeys::decode(&bytes).map_err(|e| {
serde::de::Error::custom(format!("Failed to decode ohttp keys: {}", e))
})
})
.map(Some),
}
}

0 comments on commit b9cb530

Please sign in to comment.