You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by su...@apache.org on 2024/01/04 09:58:19 UTC
(incubator-opendal) branch etcd-error created (now 5133002225)
This is an automated email from the ASF dual-hosted git repository.
suyanhanx pushed a change to branch etcd-error
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
at 5133002225 refactor(services/etcd): Impl parse_error instead of From<Error>
This branch includes the following new commits:
new 5133002225 refactor(services/etcd): Impl parse_error instead of From<Error>
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(incubator-opendal) 01/01: refactor(services/etcd): Impl parse_error instead of From
Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
suyanhanx pushed a commit to branch etcd-error
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit 5133002225624ee0309ff5cbe4da17ed08852eb6
Author: suyanhanx <su...@gmail.com>
AuthorDate: Thu Jan 4 17:57:24 2024 +0800
refactor(services/etcd): Impl parse_error instead of From<Error>
Signed-off-by: suyanhanx <su...@gmail.com>
---
core/src/services/etcd/backend.rs | 32 ++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/core/src/services/etcd/backend.rs b/core/src/services/etcd/backend.rs
index 79f64caf51..7ca456475d 100644
--- a/core/src/services/etcd/backend.rs
+++ b/core/src/services/etcd/backend.rs
@@ -273,11 +273,15 @@ impl bb8::ManageConnection for Manager {
type Error = Error;
async fn connect(&self) -> std::result::Result<Self::Connection, Self::Error> {
- Ok(Client::connect(self.endpoints.clone(), Some(self.options.clone())).await?)
+ Ok(
+ Client::connect(self.endpoints.clone(), Some(self.options.clone()))
+ .await
+ .map_err(format_etcd_error)?,
+ )
}
async fn is_valid(&self, conn: &mut Self::Connection) -> std::result::Result<(), Self::Error> {
- let _ = conn.status().await?;
+ let _ = conn.status().await.map_err(format_etcd_error)?;
Ok(())
}
@@ -347,7 +351,7 @@ impl kv::Adapter for Adapter {
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>> {
let mut client = self.conn().await?;
- let resp = client.get(key, None).await?;
+ let resp = client.get(key, None).await.map_err(format_etcd_error)?;
if let Some(kv) = resp.kvs().first() {
Ok(Some(kv.value().to_vec()))
} else {
@@ -357,20 +361,26 @@ impl kv::Adapter for Adapter {
async fn set(&self, key: &str, value: &[u8]) -> Result<()> {
let mut client = self.conn().await?;
- let _ = client.put(key, value, None).await?;
+ let _ = client
+ .put(key, value, None)
+ .await
+ .map_err(format_etcd_error)?;
Ok(())
}
async fn delete(&self, key: &str) -> Result<()> {
let mut client = self.conn().await?;
- let _ = client.delete(key, None).await?;
+ let _ = client.delete(key, None).await.map_err(format_etcd_error)?;
Ok(())
}
async fn scan(&self, path: &str) -> Result<Vec<String>> {
let mut client = self.conn().await?;
let get_options = Some(GetOptions::new().with_prefix().with_keys_only());
- let resp = client.get(path, get_options).await?;
+ let resp = client
+ .get(path, get_options)
+ .await
+ .map_err(format_etcd_error)?;
let mut res = Vec::default();
for kv in resp.kvs() {
let v = kv.key_str().map(String::from).map_err(|err| {
@@ -387,10 +397,8 @@ impl kv::Adapter for Adapter {
}
}
-impl From<EtcdError> for Error {
- fn from(e: EtcdError) -> Self {
- Error::new(ErrorKind::Unexpected, e.to_string().as_str())
- .set_source(e)
- .set_temporary()
- }
+pub fn format_etcd_error(e: EtcdError) -> Error {
+ Error::new(ErrorKind::Unexpected, e.to_string().as_str())
+ .set_source(e)
+ .set_temporary()
}