use std::borrow::Cow;
use std::fmt;
#[cfg(feature = "http")]
use super::http::HttpErrorKind;
#[cfg(feature = "db")]
use super::db::DatabaseErrorKind;
#[cfg(feature = "storage")]
use super::storage::StorageErrorKind;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
NotFound,
PermissionDenied,
Timeout,
Validation,
Unexpected,
#[cfg(feature = "http")]
Http(HttpErrorKind),
#[cfg(feature = "db")]
Database(DatabaseErrorKind),
#[cfg(feature = "storage")]
Storage(StorageErrorKind),
}
impl ErrorKind {
#[inline]
pub fn is_retryable(&self) -> bool {
match self {
ErrorKind::NotFound => false,
ErrorKind::PermissionDenied => false,
ErrorKind::Timeout => true,
ErrorKind::Validation => false,
ErrorKind::Unexpected => false,
#[cfg(feature = "http")]
ErrorKind::Http(k) => k.is_retryable(),
#[cfg(feature = "db")]
ErrorKind::Database(k) => k.is_retryable(),
#[cfg(feature = "storage")]
ErrorKind::Storage(k) => k.is_retryable(),
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorKind::NotFound => write!(f, "not found"),
ErrorKind::PermissionDenied => write!(f, "permission denied"),
ErrorKind::Timeout => write!(f, "timeout"),
ErrorKind::Validation => write!(f, "validation error"),
ErrorKind::Unexpected => write!(f, "unexpected error"),
#[cfg(feature = "http")]
ErrorKind::Http(k) => write!(f, "http error: {k}"),
#[cfg(feature = "db")]
ErrorKind::Database(k) => write!(f, "database error: {k}"),
#[cfg(feature = "storage")]
ErrorKind::Storage(k) => write!(f, "storage error: {k}"),
}
}
}
impl ErrorKind {
#[inline]
pub fn to_machine_string(&self) -> Cow<'static, str> {
match self {
ErrorKind::NotFound => Cow::Borrowed("not_found"),
ErrorKind::PermissionDenied => Cow::Borrowed("permission_denied"),
ErrorKind::Timeout => Cow::Borrowed("timeout"),
ErrorKind::Validation => Cow::Borrowed("validation_error"),
ErrorKind::Unexpected => Cow::Borrowed("unexpected_error"),
#[cfg(feature = "http")]
ErrorKind::Http(k) => Cow::Owned(format!("http_{}", k.to_machine_string())),
#[cfg(feature = "db")]
ErrorKind::Database(k) => Cow::Owned(format!("database_{}", k.to_machine_string())),
#[cfg(feature = "storage")]
ErrorKind::Storage(k) => Cow::Owned(format!("storage_{}", k.to_machine_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_not_found_is_not_retryable() {
assert!(!ErrorKind::NotFound.is_retryable());
}
#[test]
fn test_permission_denied_is_not_retryable() {
assert!(!ErrorKind::PermissionDenied.is_retryable());
}
#[test]
fn test_timeout_is_retryable() {
assert!(ErrorKind::Timeout.is_retryable());
}
#[test]
fn test_validation_is_not_retryable() {
assert!(!ErrorKind::Validation.is_retryable());
}
#[test]
fn test_unexpected_is_not_retryable() {
assert!(!ErrorKind::Unexpected.is_retryable());
}
#[test]
fn test_display() {
assert_eq!(ErrorKind::NotFound.to_string(), "not found");
assert_eq!(ErrorKind::PermissionDenied.to_string(), "permission denied");
assert_eq!(ErrorKind::Timeout.to_string(), "timeout");
assert_eq!(ErrorKind::Validation.to_string(), "validation error");
assert_eq!(ErrorKind::Unexpected.to_string(), "unexpected error");
}
}