hc_vault/
errors.rs

1use std::fmt;
2
3/// The Error
4#[derive(Debug)]
5pub enum Error {
6    /// ParseError is returned when there was an error parsing a url
7    ParseError(url::ParseError),
8    /// ReqwestError is returned when the request made to vault itself fails
9    ReqwestError(reqwest::Error),
10    /// IOError is returned by operations that have to do some sort of IO, like
11    /// the helper function for the kubernetes backend, which loads the JWT token
12    /// from a local file
13    IOError(std::io::Error),
14    /// InvalidRequest is returned when the made to vault was missing data or was invalid/
15    /// malformed data and therefore was rejected by vault before doing anything
16    InvalidRequest,
17    /// IsSealed is returned when the given vault instance is not available because it
18    /// is currently sealed and therefore does not accept or handle any requests other
19    /// than to unseal it
20    IsSealed,
21    /// NotFound is returned when the given vault endpoint/path was not found on the
22    /// actual vault instance that you are connected to
23    NotFound,
24    /// Unauthorized is returned when your current Session has either expired and has not
25    /// been renewed or when the credentials for login are not valid and therefore rejected
26    /// or when you try to access something that you dont have the permissions to do so
27    Unauthorized,
28    /// SessionExpired is returned when the session you tried to use is expired and was
29    /// configured to not automatically obtain a new session, when it notices that the
30    /// current one is expired
31    SessionExpired,
32    /// Other simply represents all other errors that could not be grouped into on the other
33    /// categories listed above
34    Other,
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        match *self {
40            Error::ParseError(ref cause) => write!(f, "Parse Error: {}", cause),
41            Error::ReqwestError(ref cause) => write!(f, "Reqwest Error: {}", cause),
42            Error::IOError(ref cause) => write!(f, "IO Error: {}", cause),
43            Error::InvalidRequest => write!(f, "Invalid Request: Invalid or Missing data"),
44            Error::IsSealed => write!(
45                f,
46                "The Vault instance is still sealed and can't be used at the moment"
47            ),
48            Error::NotFound => write!(f, "Not Found"),
49            Error::Unauthorized => write!(f, "Unauthorized"),
50            Error::SessionExpired => write!(f, "Session has expired, no auto login"),
51            Error::Other => write!(f, "Unknown error"),
52        }
53    }
54}
55
56impl From<url::ParseError> for Error {
57    fn from(cause: url::ParseError) -> Error {
58        Error::ParseError(cause)
59    }
60}
61impl From<reqwest::Error> for Error {
62    fn from(cause: reqwest::Error) -> Error {
63        Error::ReqwestError(cause)
64    }
65}
66impl From<std::io::Error> for Error {
67    fn from(cause: std::io::Error) -> Error {
68        Error::IOError(cause)
69    }
70}
71/// This is only meant for status codes and assumes that the
72/// given u16 is a status-code from an http-request
73impl From<u16> for Error {
74    fn from(cause: u16) -> Error {
75        match cause {
76            400 => Error::InvalidRequest,
77            403 => Error::Unauthorized,
78            404 => Error::NotFound,
79            503 => Error::IsSealed,
80            _ => Error::Other,
81        }
82    }
83}
84
85/// The possible errors returned by the Renew part of the Client
86pub enum RenewError {
87    /// Possible Errors returned by the Auth backend when you try to renew the
88    /// current token/session
89    AuthError(Error),
90    /// This is returned if you try to run the Renew session part but without
91    /// enabling the Renew Policy in the config
92    NotEnabled,
93    /// Returned when the current session can actually not be renewed
94    NotRenewable,
95}
96
97impl fmt::Display for RenewError {
98    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99        match *self {
100            RenewError::AuthError(ref cause) => {
101                write!(f, "Error returned by Auth-Backend: {}", cause)
102            }
103            RenewError::NotEnabled => write!(f, "The Renew Policy is not enabled"),
104            RenewError::NotRenewable => write!(f, "The current session can not be renewed"),
105        }
106    }
107}
108
109impl From<Error> for RenewError {
110    fn from(cause: Error) -> RenewError {
111        RenewError::AuthError(cause)
112    }
113}