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    InvalidHeaderValue(http::header::InvalidHeaderValue),
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
52impl Display for Error {
53    #[inline]
54    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55        match self {
56            Error::InvalidArgs(e) => write!(f, "invalid arguments: {e}"),
57            Error::InvalidUri(e) => write!(f, "invalid uri: {e}"),
58            Error::IoError(e) => write!(f, "io error: {e}"),
59            Error::TransportError(e) => write!(f, "transport error: {e}"),
60            Error::GRpcStatus(e) => write!(f, "grpc request error: {e}"),
61            Error::WatchError(e) => write!(f, "watch error: {e}"),
62            Error::Utf8Error(e) => write!(f, "utf8 error: {e}"),
63            Error::LeaseKeepAliveError(e) => write!(f, "lease keep alive error: {e}"),
64            Error::ElectError(e) => write!(f, "election error: {e}"),
65            Error::InvalidHeaderValue(e) => write!(f, "invalid metadata value: {e}"),
66            Error::EndpointError(e) => write!(f, "endpoint error: {e}"),
67            Error::EndpointsNotManaged => write!(f, "endpoints not managed by this client"),
68            #[cfg(feature = "tls-openssl")]
69            Error::OpenSsl(e) => write!(f, "open ssl error: {e}"),
70        }
71    }
72}
73
74impl std::error::Error for Error {}
75
76impl From<http::uri::InvalidUri> for Error {
77    #[inline]
78    fn from(e: http::uri::InvalidUri) -> Self {
79        Error::InvalidUri(e)
80    }
81}
82
83impl From<std::io::Error> for Error {
84    #[inline]
85    fn from(e: std::io::Error) -> Self {
86        Error::IoError(e)
87    }
88}
89
90impl From<tonic::transport::Error> for Error {
91    #[inline]
92    fn from(e: tonic::transport::Error) -> Self {
93        Error::TransportError(e)
94    }
95}
96
97impl From<tonic::Status> for Error {
98    #[inline]
99    fn from(e: tonic::Status) -> Self {
100        Error::GRpcStatus(e)
101    }
102}
103
104impl From<Utf8Error> for Error {
105    #[inline]
106    fn from(e: Utf8Error) -> Self {
107        Error::Utf8Error(e)
108    }
109}
110
111impl From<http::header::InvalidHeaderValue> for Error {
112    #[inline]
113    fn from(e: http::header::InvalidHeaderValue) -> Self {
114        Error::InvalidHeaderValue(e)
115    }
116}
117
118#[cfg(feature = "tls-openssl")]
119impl From<openssl::error::ErrorStack> for Error {
120    #[inline]
121    fn from(e: openssl::error::ErrorStack) -> Self {
122        Self::OpenSsl(e)
123    }
124}