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
//! Etcd Client Error handling.

use std::fmt::{Display, Formatter};
use std::str::Utf8Error;

pub type Result<T> = std::result::Result<T, Error>;

/// The error type for `etcd` client.
#[derive(Debug)]
pub enum Error {
    /// Invalid arguments
    InvalidArgs(String),

    /// Invalid URI
    InvalidUri(http::uri::InvalidUri),

    /// IO error
    IoError(std::io::Error),

    /// Transport error
    TransportError(tonic::transport::Error),

    /// gRPC status
    GRpcStatus(tonic::Status),

    /// Watch error
    WatchError(String),

    /// Utf8Error
    Utf8Error(Utf8Error),

    /// Lease error
    LeaseKeepAliveError(String),

    /// Election error
    ElectError(String),

    /// Invalid header value
    InvalidHeaderValue(http::header::InvalidHeaderValue),

    /// Endpoint error
    EndpointError(String),

    /// OpenSSL errors.
    #[cfg(feature = "tls-openssl")]
    OpenSsl(openssl::error::ErrorStack),
}

impl Display for Error {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::InvalidArgs(e) => write!(f, "invalid arguments: {}", e),
            Error::InvalidUri(e) => write!(f, "invalid uri: {}", e),
            Error::IoError(e) => write!(f, "io error: {}", e),
            Error::TransportError(e) => write!(f, "transport error: {}", e),
            Error::GRpcStatus(e) => write!(f, "grpc request error: {}", e),
            Error::WatchError(e) => write!(f, "watch error: {}", e),
            Error::Utf8Error(e) => write!(f, "utf8 error: {}", e),
            Error::LeaseKeepAliveError(e) => write!(f, "lease keep alive error: {}", e),
            Error::ElectError(e) => write!(f, "election error: {}", e),
            Error::InvalidHeaderValue(e) => write!(f, "invalid metadata value: {}", e),
            Error::EndpointError(e) => write!(f, "endpoint error: {}", e),
            #[cfg(feature = "tls-openssl")]
            Error::OpenSsl(e) => write!(f, "open ssl error: {}", e),
        }
    }
}

impl std::error::Error for Error {}

impl From<http::uri::InvalidUri> for Error {
    #[inline]
    fn from(e: http::uri::InvalidUri) -> Self {
        Error::InvalidUri(e)
    }
}

impl From<std::io::Error> for Error {
    #[inline]
    fn from(e: std::io::Error) -> Self {
        Error::IoError(e)
    }
}

impl From<tonic::transport::Error> for Error {
    #[inline]
    fn from(e: tonic::transport::Error) -> Self {
        Error::TransportError(e)
    }
}

impl From<tonic::Status> for Error {
    #[inline]
    fn from(e: tonic::Status) -> Self {
        Error::GRpcStatus(e)
    }
}

impl From<Utf8Error> for Error {
    #[inline]
    fn from(e: Utf8Error) -> Self {
        Error::Utf8Error(e)
    }
}

impl From<http::header::InvalidHeaderValue> for Error {
    #[inline]
    fn from(e: http::header::InvalidHeaderValue) -> Self {
        Error::InvalidHeaderValue(e)
    }
}

#[cfg(feature = "tls-openssl")]
impl From<openssl::error::ErrorStack> for Error {
    #[inline]
    fn from(e: openssl::error::ErrorStack) -> Self {
        Self::OpenSsl(e)
    }
}