From 167fc3e2ff0ea29aeb1730a3eb6a40e0275bbccf Mon Sep 17 00:00:00 2001 From: Suyan Date: Sun, 7 Jan 2024 20:14:19 +0800 Subject: [PATCH] refactor(services/redis): Impl parse_error instead of From (#3938) --- core/src/services/redis/backend.rs | 45 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/core/src/services/redis/backend.rs b/core/src/services/redis/backend.rs index 8d427f536ddd..77ca74b64c49 100644 --- a/core/src/services/redis/backend.rs +++ b/core/src/services/redis/backend.rs @@ -222,7 +222,7 @@ impl Builder for RedisBuilder { if let Some(password) = &self.config.password { client_builder = client_builder.password(password.clone()); } - let client = client_builder.build()?; + let client = client_builder.build().map_err(format_redis_error)?; let conn = OnceCell::new(); @@ -366,7 +366,8 @@ impl Adapter { .map(RedisConnection::Cluster) } }) - .await? + .await + .map_err(format_redis_error)? .clone()) } } @@ -390,11 +391,11 @@ impl kv::Adapter for Adapter { let conn = self.conn().await?; match conn { RedisConnection::Normal(mut conn) => { - let bs = conn.get(key).await?; + let bs = conn.get(key).await.map_err(format_redis_error)?; Ok(bs) } RedisConnection::Cluster(mut conn) => { - let bs = conn.get(key).await?; + let bs = conn.get(key).await.map_err(format_redis_error)?; Ok(bs) } } @@ -404,17 +405,23 @@ impl kv::Adapter for Adapter { let conn = self.conn().await?; match self.default_ttl { Some(ttl) => match conn { + RedisConnection::Normal(mut conn) => conn + .set_ex(key, value, ttl.as_secs() as usize) + .await + .map_err(format_redis_error)?, + RedisConnection::Cluster(mut conn) => conn + .set_ex(key, value, ttl.as_secs() as usize) + .await + .map_err(format_redis_error)?, + }, + None => match conn { RedisConnection::Normal(mut conn) => { - conn.set_ex(key, value, ttl.as_secs() as usize).await? + conn.set(key, value).await.map_err(format_redis_error)? } RedisConnection::Cluster(mut conn) => { - conn.set_ex(key, value, ttl.as_secs() as usize).await? + conn.set(key, value).await.map_err(format_redis_error)? } }, - None => match conn { - RedisConnection::Normal(mut conn) => conn.set(key, value).await?, - RedisConnection::Cluster(mut conn) => conn.set(key, value).await?, - }, } Ok(()) } @@ -423,10 +430,10 @@ impl kv::Adapter for Adapter { let conn = self.conn().await?; match conn { RedisConnection::Normal(mut conn) => { - let _: () = conn.del(key).await?; + let _: () = conn.del(key).await.map_err(format_redis_error)?; } RedisConnection::Cluster(mut conn) => { - let _: () = conn.del(key).await?; + let _: () = conn.del(key).await.map_err(format_redis_error)?; } } Ok(()) @@ -436,20 +443,18 @@ impl kv::Adapter for Adapter { let conn = self.conn().await?; match conn { RedisConnection::Normal(mut conn) => { - conn.append(key, value).await?; + conn.append(key, value).await.map_err(format_redis_error)?; } RedisConnection::Cluster(mut conn) => { - conn.append(key, value).await?; + conn.append(key, value).await.map_err(format_redis_error)?; } } Ok(()) } } -impl From for Error { - fn from(e: RedisError) -> Self { - Error::new(ErrorKind::Unexpected, e.category()) - .set_source(e) - .set_temporary() - } +pub fn format_redis_error(e: RedisError) -> Error { + Error::new(ErrorKind::Unexpected, e.category()) + .set_source(e) + .set_temporary() }