mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-24 22:24:53 +02: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
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -189,7 +189,6 @@ dependencies = [
|
|||||||
"strum",
|
"strum",
|
||||||
"sysctl",
|
"sysctl",
|
||||||
"sysinfo",
|
"sysinfo",
|
||||||
"thiserror",
|
|
||||||
"time",
|
"time",
|
||||||
"toml_edit",
|
"toml_edit",
|
||||||
"unicode-ellipsis",
|
"unicode-ellipsis",
|
||||||
|
@ -73,11 +73,12 @@ battery = ["starship-battery"]
|
|||||||
nvidia = ["nvml-wrapper"]
|
nvidia = ["nvml-wrapper"]
|
||||||
gpu = ["nvidia"]
|
gpu = ["nvidia"]
|
||||||
zfs = []
|
zfs = []
|
||||||
logging = ["fern", "log", "time/local-offset"]
|
|
||||||
generate_schema = ["schemars", "serde_json", "strum"]
|
|
||||||
deploy = ["battery", "gpu", "zfs"]
|
deploy = ["battery", "gpu", "zfs"]
|
||||||
default = ["deploy"]
|
default = ["deploy"]
|
||||||
|
|
||||||
|
logging = ["fern", "log", "time/local-offset"]
|
||||||
|
generate_schema = ["schemars", "serde_json", "strum"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
backtrace = "0.3.73"
|
backtrace = "0.3.73"
|
||||||
@ -97,7 +98,6 @@ regex = "1.10.5"
|
|||||||
serde = { version = "1.0.203", features = ["derive"] }
|
serde = { version = "1.0.203", features = ["derive"] }
|
||||||
starship-battery = { version = "0.8.3", optional = true }
|
starship-battery = { version = "0.8.3", optional = true }
|
||||||
sysinfo = "=0.30.12"
|
sysinfo = "=0.30.12"
|
||||||
thiserror = "1.0.61"
|
|
||||||
time = { version = "0.3.36", features = ["formatting", "macros"] }
|
time = { version = "0.3.36", features = ["formatting", "macros"] }
|
||||||
toml_edit = { version = "0.22.14", features = ["serde"] }
|
toml_edit = { version = "0.22.14", features = ["serde"] }
|
||||||
tui = { version = "0.27.0", package = "ratatui" }
|
tui = { version = "0.27.0", package = "ratatui" }
|
||||||
|
13
src/app.rs
13
src/app.rs
@ -11,6 +11,7 @@ use std::{
|
|||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use anyhow::bail;
|
||||||
use concat_string::concat_string;
|
use concat_string::concat_string;
|
||||||
use data_farmer::*;
|
use data_farmer::*;
|
||||||
use filter::*;
|
use filter::*;
|
||||||
@ -26,10 +27,7 @@ use crate::{
|
|||||||
data_collection::{processes::Pid, temperature},
|
data_collection::{processes::Pid, temperature},
|
||||||
data_conversion::ConvertedData,
|
data_conversion::ConvertedData,
|
||||||
get_network_points,
|
get_network_points,
|
||||||
utils::{
|
utils::data_units::DataUnit,
|
||||||
data_units::DataUnit,
|
|
||||||
error::{BottomError, Result},
|
|
||||||
},
|
|
||||||
widgets::{ProcWidgetColumn, ProcWidgetMode},
|
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 BottomWidgetType::Proc = self.current_widget.widget_type {
|
||||||
if let Some((_, pids)) = &self.to_delete_process_list {
|
if let Some((_, pids)) = &self.to_delete_process_list {
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
@ -1491,10 +1489,7 @@ impl App {
|
|||||||
self.to_delete_process_list = None;
|
self.to_delete_process_list = None;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(BottomError::GenericError(
|
bail!("Cannot kill processes if the current widget is not the Process widget!");
|
||||||
"Cannot kill processes if the current widget is not the Process widget!"
|
|
||||||
.to_string(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
//! This file is meant to house (OS specific) implementations on how to kill
|
//! This file is meant to house (OS specific) implementations on how to kill
|
||||||
//! processes.
|
//! processes.
|
||||||
|
|
||||||
|
use anyhow::bail;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
use windows::Win32::{
|
use windows::Win32::{
|
||||||
Foundation::{CloseHandle, HANDLE},
|
Foundation::{CloseHandle, HANDLE},
|
||||||
@ -10,7 +11,6 @@ use windows::Win32::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::data_collection::processes::Pid;
|
use crate::data_collection::processes::Pid;
|
||||||
use crate::utils::error::BottomError;
|
|
||||||
|
|
||||||
/// Based from [this SO answer](https://stackoverflow.com/a/55231715).
|
/// Based from [this SO answer](https://stackoverflow.com/a/55231715).
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
@ -18,19 +18,19 @@ struct Process(HANDLE);
|
|||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
impl Process {
|
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.
|
// SAFETY: Windows API call, tread carefully with the args.
|
||||||
match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } {
|
match unsafe { OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_TERMINATE, false, pid) } {
|
||||||
Ok(process) => Ok(Process(process)),
|
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.
|
// SAFETY: Windows API call, this is safe as we are passing in the handle.
|
||||||
let result = unsafe { TerminateProcess(self.0, 1) };
|
let result = unsafe { TerminateProcess(self.0, 1) };
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
return Err("process may have already been terminated.".to_string());
|
bail!("process may have already been terminated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -49,16 +49,16 @@ impl Drop for Process {
|
|||||||
|
|
||||||
/// Kills a process, given a PID, for windows.
|
/// Kills a process, given a PID, for windows.
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
pub fn kill_process_given_pid(pid: Pid) -> crate::utils::error::Result<()> {
|
pub fn kill_process_given_pid(pid: Pid) -> anyhow::Result<()> {
|
||||||
let process = Process::open(pid as u32).map_err(BottomError::GenericError)?;
|
let process = Process::open(pid as u32)?;
|
||||||
process.kill().map_err(BottomError::GenericError)?;
|
process.kill()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Kills a process, given a PID, for UNIX.
|
/// Kills a process, given a PID, for UNIX.
|
||||||
#[cfg(target_family = "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
|
// SAFETY: the signal should be valid, and we act properly on an error (exit
|
||||||
// code not 0).
|
// code not 0).
|
||||||
let output = unsafe { libc::kill(pid, signal as i32) };
|
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."
|
_ => "Unknown error occurred."
|
||||||
};
|
};
|
||||||
|
|
||||||
return if let Some(err_code) = err_code {
|
if let Some(err_code) = err_code {
|
||||||
Err(BottomError::GenericError(format!(
|
bail!(format!("Error code {err_code} - {err}"))
|
||||||
"Error code {err_code} - {err}"
|
|
||||||
)))
|
|
||||||
} else {
|
} 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 serde::Deserialize;
|
||||||
|
|
||||||
use super::{keep_disk_entry, DiskHarvest, IoHarvest};
|
use super::{keep_disk_entry, DiskHarvest, IoHarvest};
|
||||||
use crate::{
|
use crate::data_collection::{
|
||||||
data_collection::{deserialize_xo, disks::IoData, error::CollectionResult, DataCollector},
|
deserialize_xo, disks::IoData, error::CollectionResult, DataCollector,
|
||||||
utils::error,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Default)]
|
#[derive(Deserialize, Debug, Default)]
|
||||||
|
@ -10,21 +10,33 @@ pub enum CollectionError {
|
|||||||
Unsupported,
|
Unsupported,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CollectionError {
|
impl std::fmt::Display for CollectionError {
|
||||||
// pub(crate) fn general<E: Into<anyhow::Error>>(error: E) -> Self {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
// Self::General(error.into())
|
match self {
|
||||||
// }
|
CollectionError::General(err) => err.fmt(f),
|
||||||
|
CollectionError::Unsupported => {
|
||||||
pub(crate) fn from_str(msg: &'static str) -> Self {
|
write!(
|
||||||
Self::General(anyhow!(msg))
|
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`].
|
/// A [`Result`] with the error type being a [`DataCollectionError`].
|
||||||
pub(crate) type CollectionResult<T> = Result<T, CollectionError>;
|
pub(crate) type CollectionResult<T> = Result<T, CollectionError>;
|
||||||
|
|
||||||
impl From<std::io::Error> for CollectionError {
|
impl From<std::io::Error> for CollectionError {
|
||||||
fn from(err: std::io::Error) -> Self {
|
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"))] {
|
} else if #[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "windows", target_os = "android", target_os = "ios"))] {
|
||||||
sysinfo_process_data(self)
|
sysinfo_process_data(self)
|
||||||
} else {
|
} 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) };
|
let passwd = unsafe { libc::getpwuid(uid) };
|
||||||
|
|
||||||
if passwd.is_null() {
|
if passwd.is_null() {
|
||||||
Err(CollectionError::from_str("passwd is inaccessible"))
|
Err("passwd is inaccessible".into())
|
||||||
} else {
|
} else {
|
||||||
// SAFETY: We return early if passwd is null.
|
// SAFETY: We return early if passwd is null.
|
||||||
let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) }
|
let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) }
|
||||||
|
@ -11,7 +11,6 @@ pub mod app;
|
|||||||
pub mod utils {
|
pub mod utils {
|
||||||
pub mod data_prefixes;
|
pub mod data_prefixes;
|
||||||
pub mod data_units;
|
pub mod data_units;
|
||||||
pub mod error;
|
|
||||||
pub mod general;
|
pub mod general;
|
||||||
pub mod logging;
|
pub mod logging;
|
||||||
pub mod strings;
|
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…
x
Reference in New Issue
Block a user