1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io;

use serde_json::error::Error as SerdeError;
use packet_stream::ConnectionError;

/// An error that can be emitted during the rpc process.
#[derive(Debug)]
pub enum RpcError {
    /// An io-error that was emitted by the underlying transports or the
    /// underlying packet-stream.
    IoError(io::Error),
    /// Received a packet containing invalid data. This error is non-fatal.
    InvalidData(SerdeError),
    /// Got a packet without the json type. This error is non-fatal.
    NotJson,
}

impl Display for RpcError {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        match *self {
            RpcError::IoError(ref err) => write!(f, "Rpc error: {}", err),
            RpcError::InvalidData(ref err) => write!(f, "Rpc error: {}", err),
            RpcError::NotJson => write!(f, "Rpc error: Not json"),
        }
    }
}

impl Error for RpcError {
    fn description(&self) -> &str {
        match *self {
            RpcError::IoError(ref err) => err.description(),
            RpcError::InvalidData(ref err) => err.description(),
            RpcError::NotJson => "got a packet not of type json",
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            RpcError::IoError(ref err) => Some(err),
            RpcError::InvalidData(ref err) => Some(err),
            RpcError::NotJson => None,
        }
    }
}

impl From<io::Error> for RpcError {
    fn from(err: io::Error) -> RpcError {
        RpcError::IoError(err)
    }
}

impl From<SerdeError> for RpcError {
    fn from(err: SerdeError) -> RpcError {
        RpcError::InvalidData(err)
    }
}

/// An error that can be emitted during the rpc process when receiving multiplexed data.
#[derive(Debug)]
pub enum ConnectionRpcError<E> {
    /// An error signaled by the peer via the muxrpc protocol.
    PeerError(E),
    /// A `ConnectionError` occured.
    ConnectionError(ConnectionError),
    /// Received a packet containing invalid data. This error is non-fatal.
    InvalidData(SerdeError),
    /// Got a packet without the json type. This error is non-fatal.
    NotJson,
}

impl<E: Display> Display for ConnectionRpcError<E> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        match *self {
            ConnectionRpcError::PeerError(ref err) => {
                write!(f, "Connection rpc error: Peer error: {}", err)
            }
            ConnectionRpcError::ConnectionError(ref err) => {
                write!(f, "Connection rpc error: Connection error: {}", err)
            }
            ConnectionRpcError::InvalidData(ref err) => {
                write!(f, "Connection rpc error: Invalid data: {}", err)
            }
            ConnectionRpcError::NotJson => write!(f, "Connection rpc error: Not json"),
        }
    }
}

impl<E: Error> Error for ConnectionRpcError<E> {
    fn description(&self) -> &str {
        match *self {
            ConnectionRpcError::PeerError(ref err) => err.description(),
            ConnectionRpcError::ConnectionError(ref err) => err.description(),
            ConnectionRpcError::InvalidData(ref err) => err.description(),
            ConnectionRpcError::NotJson => "got a packet not of type json",
        }
    }

    fn cause(&self) -> Option<&Error> {
        match *self {
            ConnectionRpcError::PeerError(ref err) => Some(err),
            ConnectionRpcError::ConnectionError(ref err) => Some(err),
            ConnectionRpcError::InvalidData(ref err) => Some(err),
            ConnectionRpcError::NotJson => None,
        }
    }
}

impl<E> From<ConnectionError> for ConnectionRpcError<E> {
    fn from(err: ConnectionError) -> ConnectionRpcError<E> {
        ConnectionRpcError::ConnectionError(err)
    }
}

impl<E> From<SerdeError> for ConnectionRpcError<E> {
    fn from(err: SerdeError) -> ConnectionRpcError<E> {
        ConnectionRpcError::InvalidData(err)
    }
}