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