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

59 lines
2.1 KiB
Rust
Raw Normal View History

2021-08-06 17:58:40 +00:00
use std::{error, io};
use jsonrpc_core::{types::error::Error, ErrorCode};
use probes::ProbeError;
use snafu::Snafu;
pub type BoxError = Box<dyn error::Error>;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum StatError {
#[snafu(display("Failed to retrieve CPU statistics: {}", source))]
ReadCpuStat { source: ProbeError },
#[snafu(display("Failed to retrieve disk usage statistics: {}", source))]
ReadDiskUsage { source: ProbeError },
#[snafu(display("Failed to retrieve load average statistics: {}", source))]
ReadLoadAvg { source: ProbeError },
#[snafu(display("Failed to retrieve memory statistics: {}", source))]
ReadMemStat { source: ProbeError },
#[snafu(display("Failed to retrieve system uptime: {}", source))]
ReadUptime { source: io::Error },
}
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,
},
}
}
}