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

97 lines
3.0 KiB
Rust
Raw Permalink Normal View History

2021-11-23 08:51:54 +00:00
use std::{error, fmt};
2021-10-25 09:30:43 +00:00
2021-11-23 08:51:54 +00:00
use jsonrpc_core::types::error::Error as JsonRpcError;
use jsonrpc_core::ErrorCode;
use linux_embedded_hal::i2cdev::linux::LinuxI2CError;
2021-10-25 09:30:43 +00:00
2021-11-23 08:51:54 +00:00
#[derive(Debug)]
2021-10-25 09:30:43 +00:00
pub enum OledError {
I2CError {
2021-11-23 08:51:54 +00:00
source: LinuxI2CError,
2021-10-25 09:30:43 +00:00
},
InvalidCoordinate {
coord: String,
range: String,
value: i32,
},
2021-11-23 08:51:54 +00:00
InvalidFontSize {
font: String,
},
InvalidString {
len: usize,
},
MissingParameter {
source: JsonRpcError,
},
ParseError {
source: JsonRpcError,
},
}
2021-10-25 09:30:43 +00:00
2021-11-23 08:51:54 +00:00
impl error::Error for OledError {}
2021-10-25 09:30:43 +00:00
2021-11-23 08:51:54 +00:00
impl fmt::Display for OledError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OledError::ParseError { ref source } => {
write!(f, "Failed to parse parameter: {}", source)
}
OledError::MissingParameter { ref source } => {
write!(f, "Missing expected parameter: {}", source)
}
OledError::InvalidString { len } => {
write!(f, "String length out of range 0-21: {}", len)
}
OledError::InvalidFontSize { ref font } => {
write!(f, "Invalid font size: {}", font)
}
OledError::InvalidCoordinate {
ref coord,
ref range,
value,
} => {
write!(f, "Coordinate {} out of range {}: {}", coord, range, value)
}
OledError::I2CError { ref source } => {
write!(f, "Failed to create interface for I2C device: {}", source)
}
}
}
2021-10-25 09:30:43 +00:00
}
2021-11-23 08:51:54 +00:00
impl From<OledError> for JsonRpcError {
2021-10-25 09:30:43 +00:00
fn from(err: OledError) -> Self {
match &err {
2021-11-23 08:51:54 +00:00
OledError::I2CError { source } => JsonRpcError {
2021-10-25 09:30:43 +00:00
code: ErrorCode::ServerError(-32000),
message: format!("Failed to create interface for I2C device: {}", source),
data: None,
},
OledError::InvalidCoordinate {
coord,
value,
range,
2021-11-23 08:51:54 +00:00
} => JsonRpcError {
2021-10-25 09:30:43 +00:00
code: ErrorCode::ServerError(-32001),
message: format!(
"Validation error: coordinate {} out of range {}: {}",
coord, range, value
),
data: None,
},
2021-11-23 08:51:54 +00:00
OledError::InvalidFontSize { font } => JsonRpcError {
2021-10-25 09:30:43 +00:00
code: ErrorCode::ServerError(-32002),
message: format!("Validation error: {} is not an accepted font size. Use 6x8, 6x12, 8x16 or 12x16 instead", font),
data: None,
},
2021-11-23 08:51:54 +00:00
OledError::InvalidString { len } => JsonRpcError {
2021-10-25 09:30:43 +00:00
code: ErrorCode::ServerError(-32003),
message: format!("Validation error: string length {} out of range 0-21", len),
data: None,
},
2021-11-23 08:51:54 +00:00
OledError::MissingParameter { source } => source.clone(),
OledError::ParseError { source } => source.clone(),
2021-10-25 09:30:43 +00:00
}
}
}