diff --git a/src/bin/cib.rs b/src/bin/cib.rs index 856d4bcb..8171b984 100644 --- a/src/bin/cib.rs +++ b/src/bin/cib.rs @@ -91,8 +91,10 @@ struct ConfigCmd { } impl ConfigCmd { - fn run(&self, _args: &Args, config: &Config) -> Result<(), CibError> { - let json = config.to_json(); + fn run(&self, args: &Args, config: &Config) -> Result<(), CibError> { + let json = config + .to_json() + .map_err(|e| CibError::to_json(&args.config, e))?; if let Some(output) = &self.output { write(output, json.as_bytes()).map_err(|e| CibError::write_config(output, e))?; @@ -225,6 +227,9 @@ enum CibError { #[error("failed to write config as JSON to file {0}")] WriteConfig(PathBuf, #[source] std::io::Error), + #[error("failed to convert config as JSON")] + ToJson(PathBuf, #[source] ConfigError), + #[error("failed to look up node profile")] Profile(#[source] radicle::profile::Error), @@ -259,6 +264,10 @@ impl CibError { Self::WriteConfig(filename.into(), e) } + fn to_json(filename: &Path, e: ConfigError) -> Self { + Self::ToJson(filename.into(), e) + } + fn profile(e: radicle::profile::Error) -> Self { Self::Profile(e) } diff --git a/src/config.rs b/src/config.rs index 2298b5dd..0f0bc936 100644 --- a/src/config.rs +++ b/src/config.rs @@ -42,8 +42,8 @@ impl Config { &self.db } - pub fn to_json(&self) -> String { - serde_json::to_string_pretty(self).expect("serialize config to JSON") + pub fn to_json(&self) -> Result { + serde_json::to_string_pretty(self).map_err(ConfigError::ToJson) } } @@ -88,4 +88,8 @@ pub enum ConfigError { /// Can't parse config file as YAML. #[error("failed to parse configuration file as YAML: {0}")] ParseConfig(PathBuf, #[source] serde_yaml::Error), + + /// Can't convert configuration into JSON. + #[error("failed to convert configuration into JSON")] + ToJson(#[source] serde_json::Error), }