From a1fe7b6d5fdd66d42236032ace9cfaef21fb34fd Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Thu, 21 Mar 2019 18:18:07 +0100 Subject: [PATCH] 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. --- acmed/src/acmed.rs | 4 ++-- acmed/src/errors.rs | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/acmed/src/acmed.rs b/acmed/src/acmed.rs index 0ed45a6..6709dcf 100644 --- a/acmed/src/acmed.rs +++ b/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(()) } diff --git a/acmed/src/errors.rs b/acmed/src/errors.rs index 30042a8..1aa679e 100644 --- a/acmed/src/errors.rs +++ b/acmed/src/errors.rs @@ -24,6 +24,12 @@ impl From for Error { } } +impl From<&str> for Error { + fn from(error: &str) -> Self { + Error::new(error) + } +} + impl From for Error { fn from(error: toml::de::Error) -> Self { Error::new(&format!("IO error: {}", error))