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

70 lines
2.5 KiB
Rust

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