Skip to content

Commit

Permalink
refactor(services/redis): Impl parse_error instead of From<Error> (#3938
Browse files Browse the repository at this point in the history
)
  • Loading branch information
suyanhanx authored Jan 7, 2024
1 parent a4a7fee commit 167fc3e
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions core/src/services/redis/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -366,7 +366,8 @@ impl Adapter {
.map(RedisConnection::Cluster)
}
})
.await?
.await
.map_err(format_redis_error)?
.clone())
}
}
Expand All @@ -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)
}
}
Expand All @@ -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(())
}
Expand All @@ -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(())
Expand All @@ -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<RedisError> 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()
}

0 comments on commit 167fc3e

Please sign in to comment.