peach-workspace/peach-stats/src/error.rs

55 lines
1.9 KiB
Rust
Raw Normal View History

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;
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),
/// 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-12-09 07:44:27 +00:00
impl fmt::Display for StatsError {
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)
}
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
}
}
}