diff options
author | Neil Kollack <nkollack@gmail.com> | 2022-02-23 19:19:56 -0600 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-02-23 19:19:56 -0600 |
commit | b7bc5008d19385c837a65c5fc740586dfe27b30e (patch) | |
tree | ea27d011871656d527eca817cb3be4d8ef45df63 | |
parent | cdaaa7b896483299eae72431b7bb10dd967eb2c4 (diff) |
fix: improve error handling for container networks
-rw-r--r-- | zone_nspawn/src/container.rs | 51 | ||||
-rw-r--r-- | zone_nspawn/src/error.rs | 7 |
2 files changed, 27 insertions, 31 deletions
diff --git a/zone_nspawn/src/container.rs b/zone_nspawn/src/container.rs index a988585..f7fa37a 100644 --- a/zone_nspawn/src/container.rs +++ b/zone_nspawn/src/container.rs @@ -51,7 +51,15 @@ impl Container { fn network_configuration(&self) -> Result<()> { for (filebuf, network_config) in self.network_configs.iter() { - fs::write(filebuf, serde_ini::ser::to_vec(network_config)?)?; + fs::write( + filebuf, + serde_ini::ser::to_vec(network_config).map_err(|err| { + Error::Network(format!("Failed to serialize network config: {:?}", err)) + })?, + ) + .map_err(|err| { + Error::Network(format!("Failed to create network config file: {:?}", err)) + })?; } Ok(()) @@ -74,33 +82,24 @@ impl ContainerBuilder { } fn add_network_configs_from_config(&mut self, config: &Config) -> Result<&mut Self> { - let dir = match &config.network_configs_path { - Some(it) => it, - None => { - return Err(Error::File(format!( - "Failed to create container: {:?}", - self - ))) + if let Some(dir) = &config.network_configs_path { + if dir.is_dir() { + for entry in fs::read_dir(dir)?.flatten() { + let filename = entry.file_name(); + let file = File::open(entry.path())?; + let file_contents = match from_bufread(BufReader::new(file)) { + Ok(it) => it, + Err(err) => { + return Err(Error::Network(format!( + "Failed to parse network config: {:?}", + err + ))) + } + }; + self.add_network_config((PathBuf::from(filename), file_contents)); + } } }; - - if dir.is_dir() { - for entry in fs::read_dir(dir)?.flatten() { - let filename = entry.file_name(); - let file = File::open(entry.path())?; - let file_contents = match from_bufread(BufReader::new(file)) { - Ok(it) => it, - Err(_) => { - return Err(Error::File(format!( - "Failed to create container: {:?}", - self - ))) - } - }; - self.add_network_config((PathBuf::from(filename), file_contents)); - } - } - Ok(self) } } diff --git a/zone_nspawn/src/error.rs b/zone_nspawn/src/error.rs index a4ba175..452c042 100644 --- a/zone_nspawn/src/error.rs +++ b/zone_nspawn/src/error.rs @@ -17,15 +17,12 @@ pub enum Error { #[error("Failed to parse Command output: {0:?}")] Utf8(#[from] std::string::FromUtf8Error), - #[error("File Error: {0:?}")] - File(String), + #[error("Network Error: {0:?}")] + Network(String), #[error("Config Error: {0:?}")] Config(#[from] figment::Error), #[error("Builder Error: {0:?}")] Builder(#[from] crate::container::ContainerBuilderError), - - #[error("Serialize Error: {0:?}")] - Serialize(#[from] serde_ini::ser::Error), } |