use std::fmt;
use std::error;
use serde_json::Value;
use serde_json::error::Error as SerdeError;
#[derive(Debug)]
pub enum ClientError {
NotifyFailed,
RequestFailed,
SerializeFailed(SerdeError),
ErrorReturned(Value),
}
impl fmt::Display for ClientError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ClientError::NotifyFailed => write!(f, "Failed to send a notification"),
ClientError::RequestFailed => {
write!(f, "Failed to send a request, or receive its response")
}
ClientError::ErrorReturned(ref value) => {
write!(f, "The core returned an error: {:?}", value)
}
ClientError::SerializeFailed(ref e) => {
write!(f, "failed to serialize a message: {}", e)
}
}
}
}
impl error::Error for ClientError {
fn description(&self) -> &str {
match *self {
ClientError::NotifyFailed => "Failed to send a notification",
ClientError::RequestFailed => "Failed to send a request or receive its response",
ClientError::ErrorReturned(_) => "The core answered with an error",
ClientError::SerializeFailed(_) => "failed to serialize message",
}
}
fn cause(&self) -> Option<&error::Error> {
if let ClientError::SerializeFailed(ref serde_error) = *self {
Some(serde_error)
} else {
None
}
}
}
impl From<SerdeError> for ClientError {
fn from(err: SerdeError) -> Self {
ClientError::SerializeFailed(err)
}
}
#[derive(Debug)]
pub enum ServerError {
UnknownMethod(String),
DeserializeFailed(SerdeError),
}
impl fmt::Display for ServerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ServerError::UnknownMethod(ref method) => write!(f, "Unkown method {}", method),
ServerError::DeserializeFailed(ref e) => write!(
f,
"Failed to deserialize the parameters of a request or notification: {}",
e
),
}
}
}
impl error::Error for ServerError {
fn description(&self) -> &str {
match *self {
ServerError::UnknownMethod(_) => "Unkown method",
ServerError::DeserializeFailed(_) => {
"Failed to deserialize the parameters of a request or notification"
}
}
}
fn cause(&self) -> Option<&error::Error> {
if let ServerError::DeserializeFailed(ref serde_error) = *self {
Some(serde_error)
} else {
None
}
}
}
impl From<SerdeError> for ServerError {
fn from(err: SerdeError) -> Self {
ServerError::DeserializeFailed(err)
}
}