etcd_client/
error.rs

1//! Etcd Client Error handling.
2
3use std::fmt::{Display, Formatter};
4use std::str::Utf8Error;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// The error type for `etcd` client.
9#[derive(Debug)]
10pub enum Error {
11    /// Invalid arguments
12    InvalidArgs(String),
13
14    /// Invalid URI
15    InvalidUri(http::uri::InvalidUri),
16
17    /// IO error
18    IoError(std::io::Error),
19
20    /// Transport error
21    TransportError(tonic::transport::Error),
22
23    /// gRPC status
24    GRpcStatus(tonic::Status),
25
26    /// Watch error
27    WatchError(String),
28
29    /// Utf8Error
30    Utf8Error(Utf8Error),
31
32    /// Lease error
33    LeaseKeepAliveError(String),
34
35    /// Election error
36    ElectError(String),
37
38    /// Invalid header value
39    InvalidMetadataValue(tonic::metadata::errors::InvalidMetadataValue),
40
41    /// Endpoint error
42    EndpointError(String),
43
44    /// Endpoint set is not managed by this client
45    EndpointsNotManaged,
46
47    /// OpenSSL errors.
48    #[cfg(feature = "tls-openssl")]
49    OpenSsl(openssl::error::ErrorStack),
50
51    /// Internal error
52    Internal(String),
53}
54
55impl Display for Error {
56    #[inline]
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Error::InvalidArgs(e) => write!(f, "invalid arguments: {e}"),
60            Error::InvalidUri(e) => write!(f, "invalid uri: {e}"),
61            Error::IoError(e) => write!(f, "io error: {e}"),
62            Error::TransportError(e) => write!(f, "transport error: {e}"),
63            Error::GRpcStatus(e) => write!(f, "grpc request error: {e}"),
64            Error::WatchError(e) => write!(f, "watch error: {e}"),
65            Error::Utf8Error(e) => write!(f, "utf8 error: {e}"),
66            Error::LeaseKeepAliveError(e) => write!(f, "lease keep alive error: {e}"),
67            Error::ElectError(e) => write!(f, "election error: {e}"),
68            Error::InvalidMetadataValue(e) => write!(f, "invalid metadata value: {e}"),
69            Error::EndpointError(e) => write!(f, "endpoint error: {e}"),
70            Error::EndpointsNotManaged => write!(f, "endpoints not managed by this client"),
71            #[cfg(feature = "tls-openssl")]
72            Error::OpenSsl(e) => write!(f, "open ssl error: {e}"),
73            Error::Internal(e) => write!(f, "internal error: {e}"),
74        }
75    }
76}
77
78impl std::error::Error for Error {}
79
80impl From<http::uri::InvalidUri> for Error {
81    #[inline]
82    fn from(e: http::uri::InvalidUri) -> Self {
83        Error::InvalidUri(e)
84    }
85}
86
87impl From<std::io::Error> for Error {
88    #[inline]
89    fn from(e: std::io::Error) -> Self {
90        Error::IoError(e)
91    }
92}
93
94impl From<tonic::transport::Error> for Error {
95    #[inline]
96    fn from(e: tonic::transport::Error) -> Self {
97        Error::TransportError(e)
98    }
99}
100
101impl From<tonic::Status> for Error {
102    #[inline]
103    fn from(e: tonic::Status) -> Self {
104        Error::GRpcStatus(e)
105    }
106}
107
108impl From<Utf8Error> for Error {
109    #[inline]
110    fn from(e: Utf8Error) -> Self {
111        Error::Utf8Error(e)
112    }
113}
114
115impl From<tonic::metadata::errors::InvalidMetadataValue> for Error {
116    #[inline]
117    fn from(e: tonic::metadata::errors::InvalidMetadataValue) -> Self {
118        Error::InvalidMetadataValue(e)
119    }
120}
121
122#[cfg(feature = "tls-openssl")]
123impl From<openssl::error::ErrorStack> for Error {
124    #[inline]
125    fn from(e: openssl::error::ErrorStack) -> Self {
126        Self::OpenSsl(e)
127    }
128}