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

68 lines
2.5 KiB
Rust

use std::io;
use jsonrpc_core::{types::error::Error, ErrorCode};
use probes::ProbeError;
#[derive(Debug)]
pub enum StatError {
ReadCpuStat { source: ProbeError },
ReadDiskUsage { source: ProbeError },
ReadLoadAvg { source: ProbeError },
ReadMemStat { source: ProbeError },
ReadUptime { source: io::Error },
}
impl std::fmt::Display for StatError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
StatError::ReadCpuStat { ref source } => {
write!(f, "Failed to retrieve CPU statistics: {}", source)
}
StatError::ReadDiskUsage { ref source } => {
write!(f, "Failed to retrieve disk usage statistics: {}", source)
}
StatError::ReadLoadAvg { ref source } => {
write!(f, "Failed to retrieve load average statistics: {}", source)
}
StatError::ReadMemStat { ref source } => {
write!(f, "Failed to retrieve memory statistics: {}", source)
}
StatError::ReadUptime { ref source } => {
write!(f, "Failed to retrieve system uptime: {}", source)
}
}
}
}
impl From<StatError> for Error {
fn from(err: StatError) -> Self {
match &err {
StatError::ReadCpuStat { source } => Error {
code: ErrorCode::ServerError(-32001),
message: format!("Failed to retrieve CPU statistics: {}", source),
data: None,
},
StatError::ReadDiskUsage { source } => Error {
code: ErrorCode::ServerError(-32001),
message: format!("Failed to retrieve disk usage statistics: {}", source),
data: None,
},
StatError::ReadLoadAvg { source } => Error {
code: ErrorCode::ServerError(-32001),
message: format!("Failed to retrieve load average statistics: {}", source),
data: None,
},
StatError::ReadMemStat { source } => Error {
code: ErrorCode::ServerError(-32001),
message: format!("Failed to retrieve memory statistics: {}", source),
data: None,
},
StatError::ReadUptime { source } => Error {
code: ErrorCode::ServerError(-32001),
message: format!("Failed to retrieve system uptime: {}", source),
data: None,
},
}
}
}