1use thiserror::Error;
2use vfs::VfsError;
3
4#[derive(Error, Debug)]
6pub enum HttpsFSError {
7 #[error("IO error: {0}")]
9 IoError(#[from] std::io::Error),
10
11 #[error("The file or directory `{path}` could not be found")]
13 FileNotFound {
14 path: String,
16 },
17
18 #[error("The path `{path}` is invalid")]
20 InvalidPath {
21 path: String,
23 },
24
25 #[error("FileSystem error: {message}")]
27 Other {
28 message: String,
30 },
31
32 #[error("{context}, cause: {cause}")]
34 WithContext {
35 context: String,
37 #[source]
39 cause: Box<HttpsFSError>,
40 },
41
42 #[error("Functionality not supported by this filesystem")]
44 NotSupported,
45
46 #[error("Serialization/Deserialization error: {0}")]
49 SerDe(serde_json::Error),
50
51 #[error("Network error: {0}")]
53 Network(reqwest::Error),
54
55 #[error("Authentification Error: {0}")]
57 Auth(AuthError),
58
59 #[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
76pub 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}