mirror of
https://github.com/ClementTsang/bottom
synced 2024-11-10 06:34:16 +00:00
refactor: remove BottomError (#1498)
* refactor: remove BottomError * remove thiserror * some cleanup * forgot to remove this
This commit is contained in:
parent
0401f527e5
commit
29029b86fb
10 changed files with 43 additions and 55 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -189,7 +189,6 @@ dependencies = [
|
|||
"strum",
|
||||
"sysctl",
|
||||
"sysinfo",
|
||||
"thiserror",
|
||||
"time",
|
||||
"toml_edit",
|
||||
"unicode-ellipsis",
|
||||
|
|
|
@ -73,11 +73,12 @@ battery = ["starship-battery"]
|
|||
nvidia = ["nvml-wrapper"]
|
||||
gpu = ["nvidia"]
|
||||
zfs = []
|
||||
logging = ["fern", "log", "time/local-offset"]
|
||||
generate_schema = ["schemars", "serde_json", "strum"]
|
||||
deploy = ["battery", "gpu", "zfs"]
|
||||
default = ["deploy"]
|
||||
|
||||
logging = ["fern", "log", "time/local-offset"]
|
||||
generate_schema = ["schemars", "serde_json", "strum"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
backtrace = "0.3.73"
|
||||
|
@ -97,7 +98,6 @@ regex = "1.10.5"
|
|||
serde = { version = "1.0.203", features = ["derive"] }
|
||||
starship-battery = { version = "0.8.3", optional = true }
|
||||
sysinfo = "=0.30.12"
|
||||
thiserror = "1.0.61"
|
||||
time = { version = "0.3.36", features = ["formatting", "macros"] }
|
||||
toml_edit = { version = "0.22.14", features = ["serde"] }
|
||||
tui = { version = "0.27.0", package = "ratatui" }
|
||||
|
|
13
src/app.rs
13
src/app.rs
|
@ -11,6 +11,7 @@ use std::{
|
|||
time::Instant,
|
||||
};
|
||||
|
||||
use anyhow::bail;
|
||||
use concat_string::concat_string;
|
||||
use data_farmer::*;
|
||||
use filter::*;
|
||||
|
@ -26,10 +27,7 @@ use crate::{
|
|||
data_collection::{processes::Pid, temperature},
|
||||
data_conversion::ConvertedData,
|
||||
get_network_points,
|
||||
utils::{
|
||||
data_units::DataUnit,
|
||||
error::{BottomError, Result},
|
||||
},
|
||||
utils::data_units::DataUnit,
|
||||
widgets::{ProcWidgetColumn, ProcWidgetMode},
|
||||
};
|
||||
|
||||
|
@ -1469,7 +1467,7 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn kill_highlighted_process(&mut self) -> Result<()> {
|
||||
pub fn kill_highlighted_process(&mut self) -> anyhow::Result<()> {
|
||||
if let BottomWidgetType::Proc = self.current_widget.widget_type {
|
||||
if let Some((_, pids)) = &self.to_delete_process_list {
|
||||
#[cfg(target_family = "unix")]
|
||||
|
@ -1491,10 +1489,7 @@ impl App {
|
|||
self.to_delete_process_list = None;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(BottomError::GenericError(
|
||||
"Cannot kill processes if the current widget is not the Process widget!"
|
||||
.to_string(),
|
||||
))
|
||||
bail!("Cannot kill processes if the current widget is not the Process widget!");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
//! This file is meant to house (OS specific) implementations on how to kill
|
||||
//! processes.
|
||||
|
||||
use anyhow::bail;
|
||||
#[cfg(target_os = "windows")]
|
||||
use windows::Win32::{
|
||||
Foundation::{CloseHandle, HANDLE},
|
||||
|
@ -10,7 +11,6 @@ use windows::Win32::{
|
|||
};
|
||||
|
||||
use crate::data_collection::processes::Pid;
|
||||
use crate::utils::error::BottomError;
|
||||
|
||||
/// Based from [this SO answer](https://stackoverflow.com/a/55231715).
|
||||
#[cfg(target_os = "windows")]
|
||||
|
@ -18,19 +18,19 @@ struct Process(HANDLE);
|
|||
|
||||
#[cfg(target_os = "windows")]
|
||||
impl Process {
|
||||
fn open(pid: u32) -> Result<Process, String> {
|
||||
fn open(pid: u32) -> anyhow::Result<Process> {
|
||||
// SAFETY: Windows API call, tread carefully with the args.
|
||||
match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } {
|
||||
Ok(process) => Ok(Process(process)),
|
||||
Err(_) => Err("process may have already been terminated.".to_string()),
|
||||
Err(_) => bail!("process may have already been terminated."),
|
||||
}
|
||||
}
|
||||
|
||||
fn kill(self) -> Result<(), String> {
|
||||
fn kill(self) -> anyhow::Result<()> {
|
||||
// SAFETY: Windows API call, this is safe as we are passing in the handle.
|
||||
let result = unsafe { TerminateProcess(self.0, 1) };
|
||||
if result.is_err() {
|
||||
return Err("process may have already been terminated.".to_string());
|
||||
bail!("process may have already been terminated.");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -49,16 +49,16 @@ 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).map_err(BottomError::GenericError)?;
|
||||
process.kill().map_err(BottomError::GenericError)?;
|
||||
pub fn kill_process_given_pid(pid: Pid) -> anyhow::Result<()> {
|
||||
let process = Process::open(pid as u32)?;
|
||||
process.kill()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Kills a process, given a PID, for UNIX.
|
||||
#[cfg(target_family = "unix")]
|
||||
pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::Result<()> {
|
||||
pub fn kill_process_given_pid(pid: Pid, signal: usize) -> anyhow::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) };
|
||||
|
@ -73,12 +73,10 @@ pub fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::R
|
|||
_ => "Unknown error occurred."
|
||||
};
|
||||
|
||||
return if let Some(err_code) = err_code {
|
||||
Err(BottomError::GenericError(format!(
|
||||
"Error code {err_code} - {err}"
|
||||
)))
|
||||
if let Some(err_code) = err_code {
|
||||
bail!(format!("Error code {err_code} - {err}"))
|
||||
} else {
|
||||
Err(BottomError::GenericError(format!("Error code ??? - {err}")))
|
||||
bail!(format!("Error code unknown - {err}"))
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -6,9 +6,8 @@ use hashbrown::HashMap;
|
|||
use serde::Deserialize;
|
||||
|
||||
use super::{keep_disk_entry, DiskHarvest, IoHarvest};
|
||||
use crate::{
|
||||
data_collection::{deserialize_xo, disks::IoData, error::CollectionResult, DataCollector},
|
||||
utils::error,
|
||||
use crate::data_collection::{
|
||||
deserialize_xo, disks::IoData, error::CollectionResult, DataCollector,
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Debug, Default)]
|
||||
|
|
|
@ -10,21 +10,33 @@ pub enum CollectionError {
|
|||
Unsupported,
|
||||
}
|
||||
|
||||
impl CollectionError {
|
||||
// pub(crate) fn general<E: Into<anyhow::Error>>(error: E) -> Self {
|
||||
// Self::General(error.into())
|
||||
// }
|
||||
|
||||
pub(crate) fn from_str(msg: &'static str) -> Self {
|
||||
Self::General(anyhow!(msg))
|
||||
impl std::fmt::Display for CollectionError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
CollectionError::General(err) => err.fmt(f),
|
||||
CollectionError::Unsupported => {
|
||||
write!(
|
||||
f,
|
||||
"bottom does not support this type of data collection for this platform."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CollectionError {}
|
||||
|
||||
/// A [`Result`] with the error type being a [`DataCollectionError`].
|
||||
pub(crate) type CollectionResult<T> = Result<T, CollectionError>;
|
||||
|
||||
impl From<std::io::Error> for CollectionError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
CollectionError::General(err.into())
|
||||
Self::General(err.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for CollectionError {
|
||||
fn from(msg: &'static str) -> Self {
|
||||
Self::General(anyhow!(msg))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -144,7 +144,7 @@ impl DataCollector {
|
|||
} else if #[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "windows", target_os = "android", target_os = "ios"))] {
|
||||
sysinfo_process_data(self)
|
||||
} else {
|
||||
Err(error::BottomError::GenericError("Unsupported OS".to_string()))
|
||||
Err(crate::data_collection::error::CollectionError::Unsupported)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ impl UserTable {
|
|||
let passwd = unsafe { libc::getpwuid(uid) };
|
||||
|
||||
if passwd.is_null() {
|
||||
Err(CollectionError::from_str("passwd is inaccessible"))
|
||||
Err("passwd is inaccessible".into())
|
||||
} else {
|
||||
// SAFETY: We return early if passwd is null.
|
||||
let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) }
|
||||
|
|
|
@ -11,7 +11,6 @@ pub mod app;
|
|||
pub mod utils {
|
||||
pub mod data_prefixes;
|
||||
pub mod data_units;
|
||||
pub mod error;
|
||||
pub mod general;
|
||||
pub mod logging;
|
||||
pub mod strings;
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
use std::result;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
/// A type alias for handling errors related to Bottom.
|
||||
pub type Result<T> = result::Result<T, BottomError>;
|
||||
|
||||
/// An error that can occur while Bottom runs.
|
||||
#[derive(Debug, Error, PartialEq, Eq)]
|
||||
pub enum BottomError {
|
||||
/// An error to represent generic errors.
|
||||
#[error("Error, {0}")]
|
||||
GenericError(String),
|
||||
}
|
Loading…
Reference in a new issue