From 50f53dc480fda8b3daab7a34454c2dd9f3f5f991 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sat, 9 Mar 2024 15:34:57 +0100 Subject: Improve the error handling --- src/command/add_remote.rs | 5 +++-- src/command/publish.rs | 9 +++++---- src/command/publish_archive.rs | 9 +++++---- src/command/sync_down.rs | 9 +++++++-- src/command/sync_up.rs | 12 ++++++++++-- src/command/update.rs | 3 ++- 6 files changed, 32 insertions(+), 15 deletions(-) (limited to 'src/command') diff --git a/src/command/add_remote.rs b/src/command/add_remote.rs index 040e572..e9157f3 100644 --- a/src/command/add_remote.rs +++ b/src/command/add_remote.rs @@ -1,4 +1,4 @@ -use std::io::Result; +use std::io::{Error, ErrorKind::Other, Result}; use crate::configuration::Configuration; use crate::remote::add; @@ -16,7 +16,8 @@ impl super::Command for AddRemote { } fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { - let input = input.expect("You must provide a location for the remote."); + let input = input + .ok_or_else(|| Error::new(Other, "You must provide a location for the remote."))?; add(&configuration.config_directory, &configuration.remote_config, input) } diff --git a/src/command/publish.rs b/src/command/publish.rs index 207b45d..34ca0d2 100644 --- a/src/command/publish.rs +++ b/src/command/publish.rs @@ -1,4 +1,4 @@ -use std::io::Result; +use std::io::{Error, ErrorKind::Other, Result}; use crate::configuration::Configuration; use std::process::{Command, Stdio}; @@ -19,14 +19,15 @@ impl super::Command for Publish { fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { - let input = input.expect("You must provide a location to publish the blog"); + let input = input + .ok_or_else(|| Error::new(Other, "You must provide a location to publish the blog"))?; Command::new(COMMAND) .arg("--version") .stdout(Stdio::null()) .stderr(Stdio::null()) .status() - .expect("Publishing requires rsync"); + .map_err(|_| Error::new(Other, "Publishing requires rsync"))?; Command::new(COMMAND) @@ -36,7 +37,7 @@ impl super::Command for Publish { .stdout(Stdio::null()) .stderr(Stdio::null()) .status() - .expect("Publishing requires rsync"); + .map_err(|_| Error::new(Other, "Rsync failed to publish."))?; return Ok(()) } diff --git a/src/command/publish_archive.rs b/src/command/publish_archive.rs index 075421f..c727c16 100644 --- a/src/command/publish_archive.rs +++ b/src/command/publish_archive.rs @@ -1,4 +1,4 @@ -use std::io::Result; +use std::io::{Error, ErrorKind::Other, Result}; use crate::configuration::Configuration; use std::process::{Command, Stdio}; @@ -19,14 +19,15 @@ impl super::Command for PublishArchive { fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { - let input = input.expect("You must provide a location to publish the archive"); + let input = input + .ok_or_else(|| Error::new(Other, "You must provide a location to publish the archive"))?; Command::new(COMMAND) .arg("--version") .stdout(Stdio::null()) .stderr(Stdio::null()) .status() - .expect("Publishing requires rsync"); + .map_err(|_| Error::new(Other, "Publishing requires rsync"))?; Command::new(COMMAND) @@ -36,7 +37,7 @@ impl super::Command for PublishArchive { .stdout(Stdio::null()) .stderr(Stdio::null()) .status() - .expect("Publishing requires rsync"); + .map_err(|_| Error::new(Other, "Rsync failed to publish."))?; return Ok(()) } diff --git a/src/command/sync_down.rs b/src/command/sync_down.rs index bba34b2..eb8e57a 100644 --- a/src/command/sync_down.rs +++ b/src/command/sync_down.rs @@ -16,8 +16,13 @@ impl super::Command for SyncDown { } fn execute(&self, _: Option<&String>, configuration: &Configuration, command: &String) -> Result<()> { - if command == self.command() { - return sync_down(&configuration.data_directory, &configuration.remote_config); + match sync_down(&configuration.data_directory, &configuration.remote_config) { + Ok(_) => {} + Err(e) => { + if command == self.command() { + return Err(e) + } + } } return Ok(()) } diff --git a/src/command/sync_up.rs b/src/command/sync_up.rs index a5d2a4c..c44e0b0 100644 --- a/src/command/sync_up.rs +++ b/src/command/sync_up.rs @@ -15,8 +15,16 @@ impl super::Command for SyncUp { vec![] } - fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { - sync_up(&configuration.data_directory, &configuration.remote_config) + fn execute(&self, _: Option<&String>, configuration: &Configuration, command: &String) -> Result<()> { + match sync_up(&configuration.data_directory, &configuration.remote_config) { + Ok(_) => {} + Err(e) => { + if command == self.command() { + return Err(e) + } + } + } + return Ok(()) } fn after_dependencies(&self) -> Vec> { diff --git a/src/command/update.rs b/src/command/update.rs index 67cc462..8a3d6de 100644 --- a/src/command/update.rs +++ b/src/command/update.rs @@ -55,7 +55,8 @@ impl super::Command for Update { } fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { - let input = input.expect("You must provide a path to a post"); + let input = input + .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "You must provide a path to a post"))?; let post_location = PathBuf::from(input); if !post_location.exists() { return Err(Error::new(ErrorKind::NotFound, "The path provided does not exist")); -- cgit