Skip to content

Commit b9cb530

Browse files
authored
Propagate error if the fs read for ohttp_keys fails during config (payjoin#435)
Fixes payjoin#398
2 parents e8f1069 + 31bf322 commit b9cb530

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

payjoin-cli/src/app/config.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct AppConfig {
2020

2121
// v2 only
2222
#[cfg(feature = "v2")]
23+
#[serde(deserialize_with = "deserialize_ohttp_keys_from_path")]
2324
pub ohttp_keys: Option<payjoin::OhttpKeys>,
2425
#[cfg(feature = "v2")]
2526
pub ohttp_relay: Url,
@@ -72,7 +73,8 @@ impl AppConfig {
7273
"ohttp_relay",
7374
matches.get_one::<Url>("ohttp_relay").map(|s| s.as_str()),
7475
)?
75-
.set_default("pj_directory", "https://payjo.in")?;
76+
.set_default("pj_directory", "https://payjo.in")?
77+
.set_default("ohttp_keys", None::<String>)?;
7678

7779
let builder = match matches.subcommand() {
7880
Some(("send", _)) => builder,
@@ -101,17 +103,7 @@ impl AppConfig {
101103
)?
102104
.set_override_option(
103105
"ohttp_keys",
104-
matches.get_one::<String>("ohttp_keys").and_then(|s| {
105-
std::fs::read(s)
106-
.map_err(|e| {
107-
log::error!("Failed to read ohttp_keys file: {}", e);
108-
ConfigError::Message(format!(
109-
"Failed to read ohttp_keys file: {}",
110-
e
111-
))
112-
})
113-
.ok()
114-
}),
106+
matches.get_one::<String>("ohttp_keys").map(|s| s.as_str()),
115107
)?
116108
};
117109

@@ -135,3 +127,25 @@ impl AppConfig {
135127
Ok(app_config)
136128
}
137129
}
130+
131+
#[cfg(feature = "v2")]
132+
fn deserialize_ohttp_keys_from_path<'de, D>(
133+
deserializer: D,
134+
) -> Result<Option<payjoin::OhttpKeys>, D::Error>
135+
where
136+
D: serde::Deserializer<'de>,
137+
{
138+
let path_str: Option<String> = Option::deserialize(deserializer)?;
139+
140+
match path_str {
141+
None => Ok(None),
142+
Some(path) => std::fs::read(path)
143+
.map_err(|e| serde::de::Error::custom(format!("Failed to read ohttp_keys file: {}", e)))
144+
.and_then(|bytes| {
145+
payjoin::OhttpKeys::decode(&bytes).map_err(|e| {
146+
serde::de::Error::custom(format!("Failed to decode ohttp keys: {}", e))
147+
})
148+
})
149+
.map(Some),
150+
}
151+
}

0 commit comments

Comments
 (0)