2021-12-10 09:01:51 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use jsonrpc_core::{Error as JsonRpcError, ErrorCode};
|
2022-01-12 10:21:12 +00:00
|
|
|
use serde_json::error::Error as SerdeJsonError;
|
|
|
|
|
2021-12-10 09:01:51 +00:00
|
|
|
use peach_stats::StatsError;
|
|
|
|
|
|
|
|
/// Custom error type encapsulating all possible errors for a JSON-RPC server
|
|
|
|
/// and associated methods.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum JsonRpcServerError {
|
2022-01-12 10:21:12 +00:00
|
|
|
/// Failed to serialize a string from a data structure.
|
|
|
|
Serde(SerdeJsonError),
|
2021-12-10 09:01:51 +00:00
|
|
|
/// An error returned from the `peach-stats` library.
|
|
|
|
Stats(StatsError),
|
|
|
|
/// An expected JSON-RPC method parameter was not provided.
|
|
|
|
MissingParameter(JsonRpcError),
|
|
|
|
/// Failed to parse a provided JSON-RPC method parameter.
|
|
|
|
ParseParameter(JsonRpcError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for JsonRpcServerError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
JsonRpcServerError::ParseParameter(ref source) => {
|
|
|
|
write!(f, "Failed to parse parameter: {}", source)
|
|
|
|
}
|
|
|
|
JsonRpcServerError::MissingParameter(ref source) => {
|
|
|
|
write!(f, "Missing expected parameter: {}", source)
|
|
|
|
}
|
2022-01-12 10:21:12 +00:00
|
|
|
JsonRpcServerError::Serde(ref source) => {
|
|
|
|
write!(f, "{}", source)
|
|
|
|
}
|
2021-12-10 09:01:51 +00:00
|
|
|
JsonRpcServerError::Stats(ref source) => {
|
|
|
|
write!(f, "{}", source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<JsonRpcServerError> for JsonRpcError {
|
|
|
|
fn from(err: JsonRpcServerError) -> Self {
|
|
|
|
match &err {
|
2022-01-12 10:21:12 +00:00
|
|
|
JsonRpcServerError::Serde(source) => JsonRpcError {
|
|
|
|
code: ErrorCode::ServerError(-32002),
|
|
|
|
message: format!("{}", source),
|
|
|
|
data: None,
|
|
|
|
},
|
2021-12-10 09:01:51 +00:00
|
|
|
JsonRpcServerError::Stats(source) => JsonRpcError {
|
|
|
|
code: ErrorCode::ServerError(-32001),
|
|
|
|
message: format!("{}", source),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
JsonRpcServerError::MissingParameter(source) => source.clone(),
|
|
|
|
JsonRpcServerError::ParseParameter(source) => source.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|