Browse Source

Handle errors when writing to stdin

When hooks are called, there is an option to feed stdin with a custom
string. However, if any error happen, the .unwrap() causes the daemon
to panic. This fix transforms it into an error than can be handled.
pull/5/head
Rodolphe Breard 6 years ago
parent
commit
a1fe7b6d5f
  1. 4
      acmed/src/acmed.rs
  2. 6
      acmed/src/errors.rs

4
acmed/src/acmed.rs

@ -138,8 +138,8 @@ impl HookData {
.spawn()?;
if hook.stdin.is_some() {
let data_in = reg.render_template(&hook.stdin.to_owned().unwrap(), &self)?;
let stdin = cmd.stdin.as_mut().unwrap();
stdin.write_all(data_in.as_bytes()).unwrap();
let stdin = cmd.stdin.as_mut().ok_or("stdin not found")?;
stdin.write_all(data_in.as_bytes())?;
}
Ok(())
}

6
acmed/src/errors.rs

@ -24,6 +24,12 @@ impl From<std::io::Error> for Error {
}
}
impl From<&str> for Error {
fn from(error: &str) -> Self {
Error::new(error)
}
}
impl From<toml::de::Error> for Error {
fn from(error: toml::de::Error) -> Self {
Error::new(&format!("IO error: {}", error))

Loading…
Cancel
Save