Skip to content

Commit

Permalink
Switch from failure to thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
svenstaro committed Mar 20, 2021
1 parent 9da7de7 commit 9edc6b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 122 deletions.
95 changes: 2 additions & 93 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ serde = { version = "1", features = ["derive"] }
tar = "0.4.33"
futures = "0.3.13"
libflate = "1"
failure = "0.1.8"
thiserror = "1"
log = "0.4.14"
strum = "0.20.0"
strum_macros = "0.20.1"
Expand Down
45 changes: 17 additions & 28 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,65 @@
use failure::Fail;
use thiserror::Error;

#[derive(Debug, Fail)]
#[derive(Debug, Error)]
pub enum ContextualError {
/// Fully customized errors, not inheriting from any error
#[fail(display = "{}", _0)]
#[error("{0}")]
CustomError(String),

/// Any kind of IO errors
#[fail(display = "{}\ncaused by: {}", _0, _1)]
#[error("{0}\ncaused by: {1}")]
IoError(String, std::io::Error),

/// MultipartError, which might occur during file upload, when processing the multipart request fails
#[fail(display = "Failed to process multipart request\ncaused by: {}", _0)]
#[error("Failed to process multipart request\ncaused by: {0}")]
MultipartError(actix_multipart::MultipartError),

/// Any error related to an invalid path (failed to retrieve entry name, unexpected entry type, etc)
#[fail(display = "Invalid path\ncaused by: {}", _0)]
#[error("Invalid path\ncaused by: {0}")]
InvalidPathError(String),

/// This error might occur if the HTTP credential string does not respect the expected format
#[fail(
display = "Invalid format for credentials string. Expected username:password, username:sha256:hash or username:sha512:hash"
)]
#[error("Invalid format for credentials string. Expected username:password, username:sha256:hash or username:sha512:hash")]
InvalidAuthFormat,

/// This error might occure if the hash method is neither sha256 nor sha512
#[fail(
display = "{} is not a valid hashing method. Expected sha256 or sha512",
_0
)]
#[error("{0} is not a valid hashing method. Expected sha256 or sha512")]
InvalidHashMethod(String),

/// This error might occur if the HTTP auth hash password is not a valid hex code
#[fail(display = "Invalid format for password hash. Expected hex code")]
#[error("Invalid format for password hash. Expected hex code")]
InvalidPasswordHash,

/// This error might occur if the HTTP auth password exceeds 255 characters
#[fail(display = "HTTP password length exceeds 255 characters")]
#[error("HTTP password length exceeds 255 characters")]
PasswordTooLongError,

/// This error might occur if the user has unsufficient permissions to create an entry in a given directory
#[fail(display = "Insufficient permissions to create file in {}", _0)]
#[error("Insufficient permissions to create file in {0}")]
InsufficientPermissionsError(String),

/// Any error related to parsing.
#[fail(display = "Failed to parse {}\ncaused by: {}", _0, _1)]
#[error("Failed to parse {0}\ncaused by: {1}")]
ParseError(String, String),

/// This error might occur when the creation of an archive fails
#[fail(
display = "An error occured while creating the {}\ncaused by: {}",
_0, _1
)]
#[error("An error occured while creating the {0}\ncaused by: {1}")]
ArchiveCreationError(String, Box<ContextualError>),

/// This error might occur when the HTTP authentication fails
#[fail(
display = "An error occured during HTTP authentication\ncaused by: {}",
_0
)]
#[error("An error occured during HTTP authentication\ncaused by: {0}")]
HttpAuthenticationError(Box<ContextualError>),

/// This error might occur when the HTTP credentials are not correct
#[fail(display = "Invalid credentials for HTTP authentication")]
#[error("Invalid credentials for HTTP authentication")]
InvalidHttpCredentials,

/// This error might occur when an HTTP request is invalid
#[fail(display = "Invalid HTTP request\ncaused by: {}", _0)]
#[error("Invalid HTTP request\ncaused by: {0}")]
InvalidHttpRequestError(String),

/// This error might occur when trying to access a page that does not exist
#[fail(display = "Route {} could not be found", _0)]
#[error("Route {0} could not be found")]
RouteNotFoundError(String),
}

Expand Down

0 comments on commit 9edc6b3

Please sign in to comment.