Commit a3510896 authored by Alexis Janon's avatar Alexis Janon
Browse files

Error handling update: now uses prepend in most cases

parent 220c8c4a
Pipeline #4084 failed with stage
in 2 minutes and 24 seconds
......@@ -80,12 +80,11 @@ fn exec() -> Result<CommandResult> {
_ => LevelFilter::Off,
};
configure_logger(level);
let controllers = Rc::new(get_sys_fs_controllers().map_err(|err| {
Error::from(format!(
"Could not retrieve sysfs controller list: {}",
err.to_string()
))
})?);
let controllers = Rc::new(
get_sys_fs_controllers()
.map_err(Error::from)
.map_err(|err| err.prepend("Could not retrieve sysfs controller list"))?,
);
match matches.subcommand() {
("transaction", Some(sub_m)) => transaction(sub_m.value_of("JSON").unwrap(), controllers),
("show", Some(sub_m)) => show(sub_m),
......@@ -95,7 +94,8 @@ fn exec() -> Result<CommandResult> {
fn transaction(json: &str, controllers: Rc<SysFsControllerList>) -> Result<CommandResult> {
let mut commands = parsing::parse_json(json, controllers)
.map_err(|err| Error::from(format!("Could not parse command list: {}", err.to_string())))?;
.map_err(Error::from)
.map_err(|err| err.prepend("Could not parse command list"))?;
match commands.exec() {
Err(err) => {
error!(
......@@ -111,22 +111,16 @@ fn transaction(json: &str, controllers: Rc<SysFsControllerList>) -> Result<Comma
fn show(matches: &ArgMatches) -> Result<CommandResult> {
match matches.subcommand() {
("configuration", _) => {
let configuration = show::configuration().map_err(|err| {
Error::from(format!(
"Could not get serialized configuration: {}",
err.to_string()
))
})?;
let configuration = show::configuration()
.map_err(Error::from)
.map_err(|err| err.prepend("Could not get serialized configuration"))?;
println!("{}", configuration);
Ok(CommandResult::from(()))
}
("slice", Some(sub_m)) => {
let slice_info = show::slice(sub_m.value_of("JSON").unwrap()).map_err(|err| {
Error::from(format!(
"Could not get serialized slice information: {}",
err.to_string()
))
})?;
let slice_info = show::slice(sub_m.value_of("JSON").unwrap())
.map_err(Error::from)
.map_err(|err| err.prepend("Could not get serialized slice information"))?;
println!("{}", slice_info);
Ok(CommandResult::from(()))
}
......
......@@ -34,11 +34,11 @@ impl Command for CreateDir {
}
other => other,
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"Could not create directory {}: {}",
err.prepend(format!(
"Could not create directory {}",
self.path.display(),
err.to_string()
))
})
}
......@@ -60,10 +60,9 @@ impl Command for CreateDir {
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not remove directory {}: {}",
self.path.display(),
err.to_string()
err.prepend(format!(
"{{rollback}} Could not remove directory {}",
self.path.display()
))
})
}
......
......@@ -46,12 +46,11 @@ impl Command for FindGetSetValue {
self.path = Some(
self.controllers
.find_path(&self.name, &self.key)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"Could not retrieve path to key {} for slice {}: {}",
self.name,
self.key,
err.to_string()
err.prepend(format!(
"Could not retrieve path to key {} for slice {}",
self.name, self.key
))
})?,
);
......@@ -63,25 +62,16 @@ impl Command for FindGetSetValue {
return Err(err);
}
trace!("Reading from file {}", path.display());
let old_value = fs::read_to_string(path).map_err(|err| {
Error::from(format!(
"Could not read from file {}: {}",
path.display(),
err.to_string()
))
})?;
let old_value = fs::read_to_string(path)
.map_err(Error::from)
.map_err(|err| err.prepend(format!("Could not read from file {}", path.display())))?;
trace!("New value: `{}`. Old value: `{}`", self.value, old_value);
self.old_value = Some(old_value);
trace!("Writing to file {}", path.display());
fs::write(path, &self.value)
.map(|_| CommandResult::from(()))
.map_err(|err| {
Error::from(format!(
"Could not write to file {}: {}",
path.display(),
err.to_string()
))
})
.map_err(Error::from)
.map_err(|err| err.prepend(format!("Could not write to file {}", path.display())))
}
fn rollback(&mut self) -> Result<CommandResult> {
......@@ -98,16 +88,17 @@ impl Command for FindGetSetValue {
self.key,
self.name
);
self.path = Some(self.controllers.find_path(&self.name, &self.key).map_err(
|err| {
Error::from(format!(
"{{rollback}} Could not retrieve path to key {} for slice {}: {}",
self.name,
self.key,
err.to_string()
))
},
)?);
self.path = Some(
self.controllers
.find_path(&self.name, &self.key)
.map_err(Error::from)
.map_err(|err| {
err.prepend(format!(
"{{rollback}} Could not retrieve path to key {} for slice {}",
self.name, self.key
))
})?,
);
match self.path {
Some(ref value) => value,
None => return Err(Error::from(format!("{{rollback}} Missing previous path value to key {} for slice {}. (probably an internal bug, you should report it)", self.name, self.key)))
......
......@@ -34,13 +34,11 @@ impl Command for GetSetValue {
}
debug!("Reading and writing {}", self.path.display());
trace!("Reading from file {}", self.path.display());
let old_value = fs::read_to_string(&self.path).map_err(|err| {
Error::from(format!(
"Could not read from file {}: {}",
self.path.display(),
err.to_string()
))
})?;
let old_value = fs::read_to_string(&self.path)
.map_err(Error::from)
.map_err(|err| {
err.prepend(format!("Could not read from file {}", self.path.display()))
})?;
trace!(
"New value: \"{}\". Old value: \"{}\"",
self.value,
......@@ -50,13 +48,8 @@ impl Command for GetSetValue {
trace!("Writing to file {}", self.path.display());
fs::write(&self.path, &self.value)
.map(|_| CommandResult::from(()))
.map_err(|err| {
Error::from(format!(
"Could not write to file {}: {}",
self.path.display(),
err.to_string()
))
})
.map_err(Error::from)
.map_err(|err| err.prepend(format!("Could not write to file {}", self.path.display())))
}
fn rollback(&mut self) -> Result<CommandResult> {
......@@ -76,11 +69,11 @@ impl Command for GetSetValue {
trace!("{{rollback}} Writing to file {}", self.path.display());
fs::write(&self.path, old_value)
.map(|_| CommandResult::from(()))
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not write to file {}: {}",
self.path.display(),
err.to_string()
err.prepend(format!(
"{{rollback}} Could not write to file {}",
self.path.display()
))
})
}
......
......@@ -37,10 +37,9 @@ impl Command for RemoveDir {
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"Could not remove directory {}: {}",
err.prepend(format!(
"Could not remove directory {}",
self.path.display(),
err.to_string()
))
})
}
......@@ -60,11 +59,11 @@ impl Command for RemoveDir {
}
other => other,
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not create directory {}: {}",
err.prepend(format!(
"{{rollback}} Could not create directory {}",
self.path.display(),
err.to_string()
))
})
}
......
......@@ -62,15 +62,11 @@ impl Command for Exec {
fn exec(&mut self) -> Result<CommandResult> {
info!("Executing \"{}\" in slice \"{}\"", self.full_cmd, self.name);
Err(match self.list.exec() {
Ok(_) => Error::from(format!(
"Could not execute {}: {}",
self.full_cmd,
self.process_cmd.exec().to_string()
)),
Err(err) => Error::from(format!(
"Could not add current process to slice {}: {}",
Ok(_) => Error::from(self.process_cmd.exec())
.prepend(format!("Could not execute {}", self.full_cmd)),
Err(err) => err.prepend(format!(
"Could not add current process to slice {}",
self.name,
err.to_string()
)),
})
}
......
use configuration::sysfscontroller::*;
use error::*;
use libc;
use result::*;
use std::collections::HashMap;
......@@ -35,7 +36,8 @@ impl Command for ForkExec {
self.cmd, self.name
);
let pid = cvt(unsafe { libc::fork() })
.map_err(|err| Error::from(format!("Fork failed: {}", err.to_string())))?;
.map_err(Error::from)
.map_err(|err| err.prepend("Fork failed"))?;
match pid {
0 => self.exec.exec(),
_ => {
......@@ -60,9 +62,9 @@ impl Command for ForkExec {
Err(err) => match err.raw_os_error() {
Some(code) => match code {
libc::ECHILD => Ok(CommandResult::from(())),
_ => Err(Error::from(format!("Could not wait for children: {}", err.to_string()))),
_ => Err(Error::from(err).prepend("Could not wait for children")),
},
None => Err(Error::from(format!("Could not get raw os error for fork+exec. Error as reported by the standard library: {}", err.to_string()))),
None => Err(Error::from(err).prepend("Could not get raw os error for fork+exec. Error as reported by the standard library"))
},
_ => Err(Error::from("Unexpected return value for wait.")),
}
......
......@@ -52,11 +52,7 @@ impl Command for Spawn {
fn exec(&mut self) -> Result<CommandResult> {
info!("Spawning \"{}\" in slice \"{}\"", self.cmd, self.name);
if let Err(err) = self.list.exec() {
return Err(Error::from(format!(
"Could not spawn slice {}: {}",
self.name,
err.to_string()
)));
return Err(Error::from(err).prepend(format!("Could not spawn slice {}", self.name)));
}
self.remove.exec()
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment