implement custom error

This commit is contained in:
glyph 2021-11-23 10:51:54 +02:00
parent 116afe78fd
commit 8d18e712a1
1 changed files with 56 additions and 32 deletions

View File

@ -1,44 +1,68 @@
use std::error; use std::{error, fmt};
use jsonrpc_core::{types::error::Error, ErrorCode}; use jsonrpc_core::types::error::Error as JsonRpcError;
use linux_embedded_hal as hal; use jsonrpc_core::ErrorCode;
use snafu::Snafu; use linux_embedded_hal::i2cdev::linux::LinuxI2CError;
pub type BoxError = Box<dyn error::Error>; #[derive(Debug)]
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum OledError { pub enum OledError {
#[snafu(display("Failed to create interface for I2C device: {}", source))]
I2CError { I2CError {
source: hal::i2cdev::linux::LinuxI2CError, source: LinuxI2CError,
}, },
#[snafu(display("Coordinate {} out of range {}: {}", coord, range, value))]
InvalidCoordinate { InvalidCoordinate {
coord: String, coord: String,
range: String, range: String,
value: i32, value: i32,
}, },
InvalidFontSize {
// TODO: implement for validate() in src/lib.rs font: String,
#[snafu(display("Font size invalid: {}", font))] },
InvalidFontSize { font: String }, InvalidString {
len: usize,
#[snafu(display("String length out of range 0-21: {}", len))] },
InvalidString { len: usize }, MissingParameter {
source: JsonRpcError,
#[snafu(display("Missing expected parameter: {}", e))] },
MissingParameter { e: Error }, ParseError {
source: JsonRpcError,
#[snafu(display("Failed to parse parameter: {}", e))] },
ParseError { e: Error },
} }
impl From<OledError> for Error { impl error::Error for OledError {}
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)
}
}
}
}
impl From<OledError> for JsonRpcError {
fn from(err: OledError) -> Self { fn from(err: OledError) -> Self {
match &err { match &err {
OledError::I2CError { source } => Error { OledError::I2CError { source } => JsonRpcError {
code: ErrorCode::ServerError(-32000), code: ErrorCode::ServerError(-32000),
message: format!("Failed to create interface for I2C device: {}", source), message: format!("Failed to create interface for I2C device: {}", source),
data: None, data: None,
@ -47,7 +71,7 @@ impl From<OledError> for Error {
coord, coord,
value, value,
range, range,
} => Error { } => JsonRpcError {
code: ErrorCode::ServerError(-32001), code: ErrorCode::ServerError(-32001),
message: format!( message: format!(
"Validation error: coordinate {} out of range {}: {}", "Validation error: coordinate {} out of range {}: {}",
@ -55,18 +79,18 @@ impl From<OledError> for Error {
), ),
data: None, data: None,
}, },
OledError::InvalidFontSize { font } => Error { OledError::InvalidFontSize { font } => JsonRpcError {
code: ErrorCode::ServerError(-32002), code: ErrorCode::ServerError(-32002),
message: format!("Validation error: {} is not an accepted font size. Use 6x8, 6x12, 8x16 or 12x16 instead", font), message: format!("Validation error: {} is not an accepted font size. Use 6x8, 6x12, 8x16 or 12x16 instead", font),
data: None, data: None,
}, },
OledError::InvalidString { len } => Error { OledError::InvalidString { len } => JsonRpcError {
code: ErrorCode::ServerError(-32003), code: ErrorCode::ServerError(-32003),
message: format!("Validation error: string length {} out of range 0-21", len), message: format!("Validation error: string length {} out of range 0-21", len),
data: None, data: None,
}, },
OledError::MissingParameter { e } => e.clone(), OledError::MissingParameter { source } => source.clone(),
OledError::ParseError { e } => e.clone(), OledError::ParseError { source } => source.clone(),
} }
} }
} }