Commit 2ea286a9 authored by Alexis Janon's avatar Alexis Janon
Browse files

Second part of error handling refactor

parent a3510896
......@@ -11,11 +11,8 @@ pub struct SysConf {
// TODO: use the same controller as in main?
pub fn get_configuration() -> Result<SysConf> {
Ok(SysConf {
controllers: get_sys_fs_controllers().map_err(|err| {
Error::from(format!(
"Could not retrieve controllers: {}",
err.to_string()
))
})?,
controllers: get_sys_fs_controllers()
.map_err(Error::from)
.map_err(|err| err.prepend("Could not retrieve controllers"))?,
})
}
......@@ -6,44 +6,29 @@ use error::*;
use serde_json;
pub fn configuration() -> Result<String> {
let conf = configuration::get_configuration().map_err(|err| {
Error::from(format!(
"Could not retrieve configuration: {}",
err.to_string()
))
})?;
serde_json::to_string(&conf).map_err(|err| {
Error::from(format!(
"Could not serialize configuration to json: {}",
err.to_string()
))
})
let conf = configuration::get_configuration()
.map_err(Error::from)
.map_err(|err| err.prepend("Could not retrieve configuration"))?;
serde_json::to_string(&conf)
.map_err(Error::from)
.map_err(|err| err.prepend("Could not serialize configuration to json"))
}
pub fn slice(json: &str) -> Result<String> {
let conf = configuration::get_configuration().map_err(|err| {
Error::from(format!(
"Could not retrieve configuration: {}",
err.to_string()
))
})?;
let conf = configuration::get_configuration()
.map_err(Error::from)
.map_err(|err| err.prepend("Could not retrieve configuration"))?;
let list: Vec<SliceInfoRequest> = serde_json::from_str(json)
.map_err(|err| Error::from(format!("Could not deserialize json: {}", err.to_string())))?;
.map_err(Error::from)
.map_err(|err| err.prepend("Could not deserialize json"))?;
// let controllers = &configuration.controllers;
let result = list
.into_iter()
.map(|info| SliceInfoAnswer::from_request(info, &conf.controllers))
.collect::<Result<Vec<_>>>()
.map_err(|err| {
Error::from(format!(
"Could not get requested slice information: {}",
err.to_string()
))
})?;
serde_json::to_string(&result).map_err(|err| {
Error::from(format!(
"Could not serialize slice information to json: {}",
err.to_string()
))
})
.map_err(Error::from)
.map_err(|err| err.prepend("Could not get requested slice information"))?;
serde_json::to_string(&result)
.map_err(Error::from)
.map_err(|err| err.prepend("Could not serialize slice information to json"))
}
......@@ -24,21 +24,20 @@ impl SliceInfoAnswer {
.keys
.iter()
.map(|key| {
let path = controllers.find_path(&request.name, &key).map_err(|err| {
Error::from(format!(
"Could not retrieve path to key {} for slice {}: {}",
request.name,
key,
err.to_string()
))
})?;
let value = fs::read_to_string(&path).map_err(|err| {
Error::from(format!(
"Could not read from file {}: {}",
path.display(),
err.to_string()
))
})?;
let path = controllers
.find_path(&request.name, &key)
.map_err(Error::from)
.map_err(|err| {
err.prepend(format!(
"Could not retrieve path to key {} for slice {}",
request.name, key
))
})?;
let value = fs::read_to_string(&path)
.map_err(Error::from)
.map_err(|err| {
err.prepend(format!("Could not read from file {}", path.display()))
})?;
Ok((key.clone(), value))
}).collect::<Result<HashMap<_, _>>>()?;
Ok(SliceInfoAnswer {
......
......@@ -124,11 +124,11 @@ impl Command for FindGetSetValue {
trace!("{{rollback}} Writing to file {}", path.display());
fs::write(path, old_value)
.map(|_| CommandResult::from(()))
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not write to file {}: {}",
path.display(),
err.to_string()
err.prepend(format!(
"{{rollback}} Could not write to file {}",
path.display()
))
})
}
......
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