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
121
122
use openssl::error::ErrorStack;
use openssl::ssl::HandshakeError;
use protobuf::ProtobufError;
use serde_json::error::Error as SerializationError;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result};
use std::io::Error as IoError;
use std::net::TcpStream;

/// Consolidates possible error types that can occur in the OpenSSL lib.
#[derive(Debug)]
pub enum SslError {
    /// This variant includes everything related to the existing SSL connection.
    Generic(ErrorStack),
    /// This variant describes an error or intermediate state after a TLS handshake attempt.
    Handshake(HandshakeError<TcpStream>),
}

impl Display for SslError {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match *self {
            SslError::Generic(ref err) => Display::fmt(&err, f),
            SslError::Handshake(ref err) => Display::fmt(&err, f),
        }
    }
}

impl StdError for SslError {
    fn description(&self) -> &str {
        match *self {
            SslError::Generic(ref e) => e.description(),
            SslError::Handshake(ref e) => e.description(),
        }
    }

    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match *self {
            SslError::Generic(ref e) => e.source(),
            SslError::Handshake(ref e) => e.source(),
        }
    }
}

/// Consolidates possible error types that can occur in the lib.
#[derive(Debug)]
pub enum Error {
    /// This variant is used when error occurs in the lib logic.
    Internal(String),
    /// This variant includes everything related to the network connection.
    Io(IoError),
    /// This variant includes all possible errors that come from Protobuf layer.
    Protobuf(ProtobufError),
    /// This variant includes everything related to (de)serialization of incoming and outgoing
    /// messages.
    Serialization(SerializationError),
    /// This variant includes any error that comes from OpenSSL.
    Ssl(SslError),
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match *self {
            Error::Internal(ref message) => f.write_str(message),
            Error::Io(ref err) => Display::fmt(&err, f),
            Error::Protobuf(ref err) => Display::fmt(&err, f),
            Error::Serialization(ref err) => Display::fmt(&err, f),
            Error::Ssl(ref err) => Display::fmt(&err, f),
        }
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Internal(ref message) => message,
            Error::Io(ref err) => err.description(),
            Error::Protobuf(ref err) => err.description(),
            Error::Serialization(ref err) => err.description(),
            Error::Ssl(ref err) => err.description(),
        }
    }

    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match *self {
            Error::Io(ref err) => Some(err),
            Error::Protobuf(ref err) => Some(err),
            Error::Ssl(ref err) => Some(err),
            Error::Serialization(ref err) => Some(err),
            Error::Internal(_) => None,
        }
    }
}

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

impl From<ProtobufError> for Error {
    fn from(err: ProtobufError) -> Error {
        Error::Protobuf(err)
    }
}

impl From<SerializationError> for Error {
    fn from(err: SerializationError) -> Error {
        Error::Serialization(err)
    }
}

impl From<ErrorStack> for Error {
    fn from(err: ErrorStack) -> Error {
        Error::Ssl(SslError::Generic(err))
    }
}

impl From<HandshakeError<TcpStream>> for Error {
    fn from(err: HandshakeError<TcpStream>) -> Error {
        Error::Ssl(SslError::Handshake(err))
    }
}