use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum AuthError {
#[error("Missing DateTime header: {0}")]
MissingDateTime(String),
#[error("Invalid Authorization header: {0}")]
InvalidAuthorizationHeader(String),
#[error("Invalid credential: {0}")]
InvalidCredential(String),
#[error("Invalid hex signature: {0}")]
InvalidHexSignature(String),
#[error("Failed to create authentication context: {0}")]
ContextCreationError(String),
#[error("Authentication failed: {0}")]
AuthenticationFailed(String),
#[error("User not found: {0}")]
UserNotFound(String),
#[error("Invalid signature: {0}")]
InvalidSignature(String),
#[error("Invalid user status: {0}")]
InvalidUserStatus(String),
#[error("Authentication error: {0}")]
Other(String),
}
impl From<String> for AuthError {
fn from(msg: String) -> Self {
AuthError::Other(msg)
}
}
impl From<&str> for AuthError {
fn from(msg: &str) -> Self {
AuthError::Other(msg.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_error_variants() {
let errors = vec![
AuthError::AuthenticationFailed("could not authenticate".to_string()),
AuthError::ContextCreationError("could not create context".to_string()),
AuthError::InvalidAuthorizationHeader("invalid authorization header".to_string()),
AuthError::InvalidCredential("invalid credential".to_string()),
AuthError::InvalidHexSignature("invalid hex signature".to_string()),
AuthError::InvalidSignature("invalid signature".to_string()),
AuthError::InvalidUserStatus("invalid user status".to_string()),
AuthError::MissingDateTime("missing date time".to_string()),
AuthError::Other("other error".to_string()),
AuthError::UserNotFound("user not found".to_string()),
];
for error in errors {
let _msg = format!("{}", error);
let _debug = format!("{:?}", error);
}
}
#[test]
fn test_from_string_creates_other() {
let error: AuthError = String::from("custom error").into();
match error {
AuthError::Other(msg) => assert_eq!(msg, "custom error"),
_ => panic!("Expected AuthError::Other"),
}
}
#[test]
fn test_from_str_creates_other() {
let error: AuthError = "custom error".into();
match error {
AuthError::Other(msg) => assert_eq!(msg, "custom error"),
_ => panic!("Expected AuthError::Other"),
}
}
}