vfs_https/
error.rs

1use thiserror::Error;
2use vfs::VfsError;
3
4/// Internal error
5#[derive(Error, Debug)]
6pub enum HttpsFSError {
7    /// A generic IO error
8    #[error("IO error: {0}")]
9    IoError(#[from] std::io::Error),
10
11    /// The file or directory at the given path could not be found
12    #[error("The file or directory `{path}` could not be found")]
13    FileNotFound {
14        /// The path of the file not found
15        path: String,
16    },
17
18    /// The given path is invalid, e.g. because contains '.' or '..'
19    #[error("The path `{path}` is invalid")]
20    InvalidPath {
21        /// The invalid path
22        path: String,
23    },
24
25    /// Generic error variant
26    #[error("FileSystem error: {message}")]
27    Other {
28        /// The generic error message
29        message: String,
30    },
31
32    /// Generic error context, used for adding context to an error (like a path)
33    #[error("{context}, cause: {cause}")]
34    WithContext {
35        /// The context error message
36        context: String,
37        /// The underlying error
38        #[source]
39        cause: Box<HttpsFSError>,
40    },
41
42    /// Functionality not supported by this filesystem
43    #[error("Functionality not supported by this filesystem")]
44    NotSupported,
45
46    /// Error during the serialization or the de-serialisation of command 
47    /// exchanged between the [crate::HttpsFS] and the [crate::HttpsFSServer]
48    #[error("Serialization/Deserialization error: {0}")]
49    SerDe(serde_json::Error),
50
51    /// Any kind of network error (e.g. time out)
52    #[error("Network error: {0}")]
53    Network(reqwest::Error),
54
55    /// Authentication failed.
56    #[error("Authentification Error: {0}")]
57    Auth(AuthError),
58
59    /// Parsing of a http header failed.
60    #[error("Faild to parse a http header: {0}")]
61    InvalidHeader(String),
62}
63
64#[derive(Error, Debug)]
65pub enum AuthError {
66    #[error("Server didn't specified a authentification method.")]
67    NoMethodSpecified,
68    #[error("Authentification method, requested by server, is not supported.")]
69    MethodNotSupported,
70    #[error("No credential source set. (Use HttpsFS::builder().set_credential_provider()).")]
71    NoCredentialSource,
72    #[error("Faild. (Password or username wrong?)")]
73    Failed,
74}
75
76/// Shortcut type definition
77pub type HttpsFSResult<T> = std::result::Result<T, HttpsFSError>;
78
79impl From<serde_json::Error> for HttpsFSError {
80    fn from(error: serde_json::Error) -> Self {
81        HttpsFSError::SerDe(error)
82    }
83}
84
85impl From<reqwest::Error> for HttpsFSError {
86    fn from(error: reqwest::Error) -> Self {
87        HttpsFSError::Network(error)
88    }
89}
90
91impl From<HttpsFSError> for VfsError {
92    fn from(error: HttpsFSError) -> Self {
93        let cause = Box::new(match error {
94            HttpsFSError::SerDe(_) => VfsError::Other {
95                message: format!("{}", error),
96            },
97            HttpsFSError::Network(_) => VfsError::Other {
98                message: format!("{}", error),
99            },
100            HttpsFSError::Auth(_) => VfsError::Other {
101                message: format!("{}", error),
102            },
103            HttpsFSError::InvalidHeader(_) => VfsError::Other {
104                message: format!("{}", error),
105            },
106            HttpsFSError::IoError(io) => {
107                return VfsError::IoError(io);
108            }
109            HttpsFSError::FileNotFound { path } => {
110                return VfsError::FileNotFound { path };
111            }
112            HttpsFSError::InvalidPath { path } => {
113                return VfsError::InvalidPath { path };
114            }
115            HttpsFSError::Other { message } => {
116                return VfsError::Other { message };
117            }
118            HttpsFSError::WithContext { context, cause } => {
119                return VfsError::WithContext {
120                    context,
121                    cause: Box::new(VfsError::from(*cause)),
122                };
123            }
124            HttpsFSError::NotSupported => return VfsError::NotSupported,
125        });
126        VfsError::WithContext {
127            context: String::from("HttpsFS"),
128            cause,
129        }
130    }
131}
132
133impl From<VfsError> for HttpsFSError {
134    fn from(error: VfsError) -> Self {
135        match error {
136            VfsError::IoError(io) => HttpsFSError::IoError(io),
137            VfsError::FileNotFound { path } => HttpsFSError::FileNotFound { path },
138            VfsError::InvalidPath { path } => HttpsFSError::InvalidPath { path },
139            VfsError::Other { message } => HttpsFSError::Other { message },
140            VfsError::WithContext { context, cause } => HttpsFSError::WithContext {
141                context,
142                cause: Box::new(HttpsFSError::from(*cause)),
143            },
144            VfsError::NotSupported => HttpsFSError::NotSupported,
145        }
146    }
147}