From c56e28328eab6b8a1ad032630b81151de5f70e48 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Fri, 19 Jul 2024 23:52:13 -0400 Subject: [PATCH] refactor: convert more errors over (#1496) * refactor: remove From for BottomError * fix kill_process --- src/app/process_killer.rs | 9 +++------ src/canvas/styling.rs | 17 ++++++++++++----- src/utils/error.rs | 6 ------ 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/app/process_killer.rs b/src/app/process_killer.rs index 3c2aaef7..fae05dd2 100644 --- a/src/app/process_killer.rs +++ b/src/app/process_killer.rs @@ -10,7 +10,6 @@ use windows::Win32::{ }; use crate::data_collection::processes::Pid; -#[cfg(target_family = "unix")] use crate::utils::error::BottomError; /// Based from [this SO answer](https://stackoverflow.com/a/55231715). @@ -51,10 +50,8 @@ impl Drop for Process { /// Kills a process, given a PID, for windows. #[cfg(target_os = "windows")] pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> { - { - let process = Process::open(pid as u32)?; - process.kill()?; - } + let process = Process::open(pid as u32).map_err(BottomError::GenericError)?; + process.kill().map_err(BottomError::GenericError)?; Ok(()) } @@ -64,8 +61,8 @@ pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> { pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::Result<()> { // SAFETY: the signal should be valid, and we act properly on an error (exit // code not 0). - let output = unsafe { libc::kill(pid, signal as i32) }; + if output != 0 { // We had an error... let err_code = std::io::Error::last_os_error().raw_os_error(); diff --git a/src/canvas/styling.rs b/src/canvas/styling.rs index a77f88df..5794f67d 100644 --- a/src/canvas/styling.rs +++ b/src/canvas/styling.rs @@ -8,7 +8,6 @@ pub use crate::options::ConfigV1; use crate::{ constants::*, options::{colours::ColoursConfig, OptionError, OptionResult}, - utils::error, }; pub struct CanvasStyling { @@ -206,18 +205,26 @@ impl CanvasStyling { if let Some(scroll_entry_text_color) = &colours.selected_text_color { self.set_scroll_entry_text_color(scroll_entry_text_color) - .map_err(|_| OptionError::invalid_config_value("selected_text_color"))? + .map_err(|err| { + OptionError::config(format!( + "Please update 'colors.selected_text_color' in your config file. {err}", + )) + })? } if let Some(scroll_entry_bg_color) = &colours.selected_bg_color { self.set_scroll_entry_bg_color(scroll_entry_bg_color) - .map_err(|_| OptionError::invalid_config_value("selected_bg_color"))? + .map_err(|err| { + OptionError::config(format!( + "Please update 'colors.selected_bg_color' in your config file. {err}", + )) + })? } Ok(()) } - fn set_scroll_entry_text_color(&mut self, colour: &str) -> error::Result<()> { + fn set_scroll_entry_text_color(&mut self, colour: &str) -> Result<(), String> { self.currently_selected_text_colour = str_to_colour(colour)?; self.currently_selected_text_style = Style::default() .fg(self.currently_selected_text_colour) @@ -226,7 +233,7 @@ impl CanvasStyling { Ok(()) } - fn set_scroll_entry_bg_color(&mut self, colour: &str) -> error::Result<()> { + fn set_scroll_entry_bg_color(&mut self, colour: &str) -> Result<(), String> { self.currently_selected_bg_colour = str_to_colour(colour)?; self.currently_selected_text_style = Style::default() .fg(self.currently_selected_text_colour) diff --git a/src/utils/error.rs b/src/utils/error.rs index 9e162f38..ba8a92fa 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -21,9 +21,3 @@ impl From for BottomError { BottomError::InvalidIo(err.to_string()) } } - -impl From for BottomError { - fn from(err: String) -> Self { - BottomError::GenericError(err) - } -}