Skip to content

feat: Improve error handle for s3 service #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 90 additions & 24 deletions src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,34 @@ impl Accessor for Backend {

let resp = self.get_object(&p, args.offset, args.size).await?;

info!(
"object {} reader created: offset {:?}, size {:?}",
&p, args.offset, args.size
);
match resp.status() {
http::StatusCode::OK | http::StatusCode::PARTIAL_CONTENT => {
info!(
"object {} reader created: offset {:?}, size {:?}",
&p, args.offset, args.size
);

Ok(Box::new(ByteStream(resp).into_async_read()))
Ok(Box::new(ByteStream(resp).into_async_read()))
}
http::StatusCode::NOT_FOUND => Err(Error::Object {
kind: Kind::ObjectNotExist,
op: "read",
path: p.clone(),
source: anyhow!("object not found: {:?}", resp),
}),
http::StatusCode::FORBIDDEN => Err(Error::Object {
kind: Kind::ObjectPermissionDenied,
op: "read",
path: p.clone(),
source: anyhow!("object permission denied: {:?}", resp),
}),
_ => Err(Error::Object {
kind: Kind::Unexpected,
op: "read",
path: p.clone(),
source: anyhow!("object unexpected: {:?}", resp),
}),
}
}

async fn write(&self, r: BoxedAsyncReader, args: &OpWrite) -> Result<usize> {
Expand All @@ -397,11 +419,17 @@ impl Accessor for Backend {
info!("object {} write finished: size {:?}", &p, args.size);
Ok(args.size as usize)
}
http::StatusCode::FORBIDDEN => Err(Error::Object {
kind: Kind::ObjectPermissionDenied,
op: "write",
path: p.clone(),
source: anyhow!("object permission denied: {:?}", resp),
}),
_ => Err(Error::Object {
kind: Kind::Unexpected,
op: "write",
path: p.to_string(),
source: anyhow!("{:?}", resp),
source: anyhow!("object unexpected: {:?}", resp),
}),
}
}
Expand Down Expand Up @@ -481,19 +509,16 @@ impl Accessor for Backend {
kind: Kind::ObjectNotExist,
op: "stat",
path: p.to_string(),
source: anyhow!("{:?}", resp),
source: anyhow!("object not exist: {:?}", resp),
})
}
}
_ => {
error!("object {} head_object: {:?}", &p, resp);
Err(Error::Object {
kind: Kind::Unexpected,
op: "stat",
path: p.to_string(),
source: anyhow!("{:?}", resp),
})
}
_ => Err(Error::Object {
kind: Kind::Unexpected,
op: "stat",
path: p.to_string(),
source: anyhow!("object unexpected: {:?}", resp),
}),
}
}

Expand All @@ -503,10 +528,26 @@ impl Accessor for Backend {
let p = self.get_abs_path(&args.path);
info!("object {} delete start", &p);

let _ = self.delete_object(&p).await?;
let resp = self.delete_object(&p).await?;

info!("object {} delete finished", &p);
Ok(())
match resp.status() {
http::StatusCode::NO_CONTENT => {
info!("object {} delete finished", &p);
Ok(())
}
http::StatusCode::FORBIDDEN => Err(Error::Object {
kind: Kind::ObjectPermissionDenied,
op: "delete",
path: p.to_string(),
source: anyhow!("object permission denied: {:?}", resp),
}),
_ => Err(Error::Object {
kind: Kind::Unexpected,
op: "delete",
path: p.to_string(),
source: anyhow!("object unexpected: {:?}", resp),
}),
}
}

async fn list(&self, args: &OpList) -> Result<BoxedObjectStream> {
Expand Down Expand Up @@ -546,7 +587,12 @@ impl Backend {

self.client.request(req).await.map_err(|e| {
error!("object {} get_object: {:?}", path, e);
Error::Unexpected(anyhow::Error::from(e))
Error::Object {
kind: Kind::Unexpected,
op: "read",
path: path.to_string(),
source: anyhow::Error::from(e),
}
})
}

Expand All @@ -570,7 +616,12 @@ impl Backend {

self.client.request(req).await.map_err(|e| {
error!("object {} put_object: {:?}", path, e);
Error::Unexpected(anyhow::Error::from(e))
Error::Object {
kind: Kind::Unexpected,
op: "write",
path: path.to_string(),
source: anyhow::Error::from(e),
}
})
}

Expand All @@ -583,7 +634,12 @@ impl Backend {

self.client.request(req).await.map_err(|e| {
error!("object {} head_object: {:?}", path, e);
Error::Unexpected(anyhow::Error::from(e))
Error::Object {
kind: Kind::Unexpected,
op: "stat",
path: path.to_string(),
source: anyhow::Error::from(e),
}
})
}

Expand All @@ -597,7 +653,12 @@ impl Backend {

self.client.request(req).await.map_err(|e| {
error!("object {} delete_object: {:?}", path, e);
Error::Unexpected(anyhow::Error::from(e))
Error::Object {
kind: Kind::Unexpected,
op: "delete",
path: path.to_string(),
source: anyhow::Error::from(e),
}
})
}

Expand All @@ -622,7 +683,12 @@ impl Backend {

self.client.request(req).await.map_err(|e| {
error!("object {} list_object: {:?}", path, e);
Error::Unexpected(anyhow::Error::from(e))
Error::Object {
kind: Kind::Unexpected,
op: "list",
path: path.to_string(),
source: anyhow::Error::from(e),
}
})
}
}
Expand Down