From 12b11a6183c753251f85d26aac5e5f50c27d2369 Mon Sep 17 00:00:00 2001
From: RA <70325462+RAprogramm@users.noreply.github.com>
Date: Thu, 11 Sep 2025 13:08:28 +0700
Subject: [PATCH] classify Redis errors as cache
---
README.md | 16 ++++++++--------
src/convert/redis.rs | 34 +++++++++++++++++++++++++---------
2 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/README.md b/README.md
index abb64ed..96d995f 100644
--- a/README.md
+++ b/README.md
@@ -179,14 +179,14 @@ utoipa = "5"
Conversions
-- `std::io::Error` → Internal
-- `String` → BadRequest
-- `sqlx::Error` → NotFound/Database
-- `redis::RedisError` → Service
-- `reqwest::Error` → Timeout/Network/ExternalApi
-- `validator::ValidationErrors` → Validation
-- `config::ConfigError` → Config
-- `tokio::time::error::Elapsed` → Timeout
+- `std::io::Error` → Internal
+- `String` → BadRequest
+- `sqlx::Error` → NotFound/Database
+- `redis::RedisError` → Cache
+- `reqwest::Error` → Timeout/Network/ExternalApi
+- `validator::ValidationErrors` → Validation
+- `config::ConfigError` → Config
+- `tokio::time::error::Elapsed` → Timeout
diff --git a/src/convert/redis.rs b/src/convert/redis.rs
index 6465a05..8494ae3 100644
--- a/src/convert/redis.rs
+++ b/src/convert/redis.rs
@@ -4,13 +4,14 @@
//!
//! ## Mapping
//!
-//! All Redis client errors are mapped to `AppErrorKind::Service`.
+//! All Redis client errors are mapped to `AppErrorKind::Cache`.
//! The full error string from the driver is preserved in `message` for logs
//! and JSON payloads (if applicable).
//!
-//! This categorization treats Redis as an infrastructure/service dependency.
-//! If you need a stricter taxonomy (e.g. `Cache` vs `Queue`), introduce
-//! dedicated `AppErrorKind` variants and adjust the mapping accordingly.
+//! This categorization treats Redis as a cache infrastructure dependency.
+//! If you need a different taxonomy (e.g. distinguishing caches from queues),
+//! introduce dedicated `AppErrorKind` variants and adjust the mapping
+//! accordingly.
//!
//! ## Example
//!
@@ -26,7 +27,7 @@
//! let dummy = RedisError::from((redis::ErrorKind::IoError, "connection lost"));
//! let app_err = handle_cache_error(dummy);
//!
-//! assert!(matches!(app_err.kind, AppErrorKind::Service));
+//! assert!(matches!(app_err.kind, AppErrorKind::Cache));
//! ```
#[cfg(feature = "redis")]
@@ -35,15 +36,30 @@ use redis::RedisError;
#[cfg(feature = "redis")]
use crate::AppError;
-/// Map any [`redis::RedisError`] into an [`AppError`] with kind `Service`.
+/// Map any [`redis::RedisError`] into an [`AppError`] with kind `Cache`.
///
-/// Rationale: Redis is treated as a backend service/cache dependency.
+/// Rationale: Redis is treated as a backend cache dependency.
/// Detailed driver errors are kept in the message for diagnostics.
#[cfg(feature = "redis")]
#[cfg_attr(docsrs, doc(cfg(feature = "redis")))]
impl From for AppError {
fn from(err: RedisError) -> Self {
- // Infrastructure/cache issue -> service-level error for now
- AppError::service(format!("Redis error: {err}"))
+ // Infrastructure cache issue -> cache-level error
+ AppError::cache(format!("Redis error: {err}"))
+ }
+}
+
+#[cfg(all(test, feature = "redis"))]
+mod tests {
+ use redis::ErrorKind;
+
+ use super::*;
+ use crate::AppErrorKind;
+
+ #[test]
+ fn maps_to_cache_kind() {
+ let redis_err = RedisError::from((ErrorKind::IoError, "boom"));
+ let app_err: AppError = redis_err.into();
+ assert!(matches!(app_err.kind, AppErrorKind::Cache));
}
}