2021-12-09 07:44:27 +00:00
|
|
|
//! Custom error type for `peach-stats`.
|
2021-08-06 17:58:40 +00:00
|
|
|
|
|
|
|
use probes::ProbeError;
|
2022-01-25 09:36:24 +00:00
|
|
|
use std::{error, fmt, io::Error as IoError, str::Utf8Error};
|
2021-08-06 17:58:40 +00:00
|
|
|
|
2021-12-09 07:44:27 +00:00
|
|
|
/// Custom error type encapsulating all possible errors when retrieving system
|
|
|
|
/// statistics.
|
2021-10-30 10:45:09 +00:00
|
|
|
#[derive(Debug)]
|
2021-12-09 07:44:27 +00:00
|
|
|
pub enum StatsError {
|
|
|
|
/// Failed to retrieve CPU statistics.
|
|
|
|
CpuStat(ProbeError),
|
|
|
|
/// Failed to retrieve disk usage statistics.
|
|
|
|
DiskUsage(ProbeError),
|
|
|
|
/// Failed to retrieve load average statistics.
|
|
|
|
LoadAvg(ProbeError),
|
|
|
|
/// Failed to retrieve memory usage statistics.
|
|
|
|
MemStat(ProbeError),
|
|
|
|
/// Failed to retrieve system uptime.
|
|
|
|
Uptime(IoError),
|
2022-01-25 09:36:24 +00:00
|
|
|
/// Systemctl command returned an error.
|
|
|
|
Systemctl(IoError),
|
|
|
|
/// Failed to interpret sequence of `u8` as a string.
|
|
|
|
Utf8String(Utf8Error),
|
2021-08-06 17:58:40 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 07:44:27 +00:00
|
|
|
impl error::Error for StatsError {}
|
2021-11-01 12:10:16 +00:00
|
|
|
|
2021-12-09 07:44:27 +00:00
|
|
|
impl fmt::Display for StatsError {
|
2021-11-01 12:10:16 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-10-30 10:45:09 +00:00
|
|
|
match *self {
|
2021-12-09 07:44:27 +00:00
|
|
|
StatsError::CpuStat(ref source) => {
|
2021-10-30 10:45:09 +00:00
|
|
|
write!(f, "Failed to retrieve CPU statistics: {}", source)
|
|
|
|
}
|
2021-12-09 07:44:27 +00:00
|
|
|
StatsError::DiskUsage(ref source) => {
|
2021-10-30 10:45:09 +00:00
|
|
|
write!(f, "Failed to retrieve disk usage statistics: {}", source)
|
|
|
|
}
|
2021-12-09 07:44:27 +00:00
|
|
|
StatsError::LoadAvg(ref source) => {
|
2021-10-30 10:45:09 +00:00
|
|
|
write!(f, "Failed to retrieve load average statistics: {}", source)
|
|
|
|
}
|
2021-12-09 07:44:27 +00:00
|
|
|
StatsError::MemStat(ref source) => {
|
2021-10-30 10:45:09 +00:00
|
|
|
write!(f, "Failed to retrieve memory statistics: {}", source)
|
|
|
|
}
|
2021-12-09 07:44:27 +00:00
|
|
|
StatsError::Uptime(ref source) => {
|
2021-10-30 10:45:09 +00:00
|
|
|
write!(f, "Failed to retrieve system uptime: {}", source)
|
|
|
|
}
|
2022-01-25 09:36:24 +00:00
|
|
|
StatsError::Systemctl(ref source) => {
|
|
|
|
write!(f, "Systemctl command returned an error: {}", source)
|
|
|
|
}
|
|
|
|
StatsError::Utf8String(ref source) => {
|
|
|
|
write!(f, "Failed to convert stdout to string: {}", source)
|
|
|
|
}
|
2021-10-30 10:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|