use thiserror::Error;
use vfs::VfsError;
#[derive(Error, Debug)]
pub enum HttpsFSError {
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("The file or directory `{path}` could not be found")]
FileNotFound {
path: String,
},
#[error("The path `{path}` is invalid")]
InvalidPath {
path: String,
},
#[error("FileSystem error: {message}")]
Other {
message: String,
},
#[error("{context}, cause: {cause}")]
WithContext {
context: String,
#[source]
cause: Box<HttpsFSError>,
},
#[error("Functionality not supported by this filesystem")]
NotSupported,
#[error("Serialization/Deserialization error: {0}")]
SerDe(serde_json::Error),
#[error("Network error: {0}")]
Network(reqwest::Error),
#[error("Authentification Error: {0}")]
Auth(AuthError),
#[error("Faild to parse a http header: {0}")]
InvalidHeader(String),
}
#[derive(Error, Debug)]
pub enum AuthError {
#[error("Server didn't specified a authentification method.")]
NoMethodSpecified,
#[error("Authentification method, requested by server, is not supported.")]
MethodNotSupported,
#[error("No credential source set. (Use HttpsFS::builder().set_credential_provider()).")]
NoCredentialSource,
#[error("Faild. (Password or username wrong?)")]
Failed,
}
pub type HttpsFSResult<T> = std::result::Result<T, HttpsFSError>;
impl From<serde_json::Error> for HttpsFSError {
fn from(error: serde_json::Error) -> Self {
HttpsFSError::SerDe(error)
}
}
impl From<reqwest::Error> for HttpsFSError {
fn from(error: reqwest::Error) -> Self {
HttpsFSError::Network(error)
}
}
impl From<HttpsFSError> for VfsError {
fn from(error: HttpsFSError) -> Self {
let cause = Box::new(match error {
HttpsFSError::SerDe(_) => VfsError::Other {
message: format!("{}", error),
},
HttpsFSError::Network(_) => VfsError::Other {
message: format!("{}", error),
},
HttpsFSError::Auth(_) => VfsError::Other {
message: format!("{}", error),
},
HttpsFSError::InvalidHeader(_) => VfsError::Other {
message: format!("{}", error),
},
HttpsFSError::IoError(io) => {
return VfsError::IoError(io);
}
HttpsFSError::FileNotFound { path } => {
return VfsError::FileNotFound { path };
}
HttpsFSError::InvalidPath { path } => {
return VfsError::InvalidPath { path };
}
HttpsFSError::Other { message } => {
return VfsError::Other { message };
}
HttpsFSError::WithContext { context, cause } => {
return VfsError::WithContext {
context,
cause: Box::new(VfsError::from(*cause)),
};
}
HttpsFSError::NotSupported => return VfsError::NotSupported,
});
VfsError::WithContext {
context: String::from("HttpsFS"),
cause,
}
}
}
impl From<VfsError> for HttpsFSError {
fn from(error: VfsError) -> Self {
match error {
VfsError::IoError(io) => HttpsFSError::IoError(io),
VfsError::FileNotFound { path } => HttpsFSError::FileNotFound { path },
VfsError::InvalidPath { path } => HttpsFSError::InvalidPath { path },
VfsError::Other { message } => HttpsFSError::Other { message },
VfsError::WithContext { context, cause } => HttpsFSError::WithContext {
context,
cause: Box::new(HttpsFSError::from(*cause)),
},
VfsError::NotSupported => HttpsFSError::NotSupported,
}
}
}