diff --git a/.gitignore b/.gitignore index 9ba3f62b360..577932e13ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# IDE and editor +.vscode +.idea + **/target /Cargo.lock diff --git a/src/http_util/error.rs b/src/http_util/error.rs index 92d37f51f66..bde7b57adab 100644 --- a/src/http_util/error.rs +++ b/src/http_util/error.rs @@ -57,6 +57,19 @@ pub fn parse_error_kind(err: &isahc::Error) -> ErrorKind { } } +/// parse_error_status_code will parse HTTP status code into `ErrorKind` +pub fn parse_error_status_code(code: StatusCode) -> ErrorKind { + match code { + StatusCode::NOT_FOUND => ErrorKind::NotFound, + StatusCode::FORBIDDEN => ErrorKind::PermissionDenied, + StatusCode::INTERNAL_SERVER_ERROR + | StatusCode::BAD_GATEWAY + | StatusCode::SERVICE_UNAVAILABLE + | StatusCode::GATEWAY_TIMEOUT => ErrorKind::Interrupted, + _ => ErrorKind::Other, + } +} + /// parse_error_response will try to read and parse error response. pub fn parse_error_response( op: &'static str, diff --git a/src/http_util/mod.rs b/src/http_util/mod.rs index dae1e877f0a..509afb951b2 100644 --- a/src/http_util/mod.rs +++ b/src/http_util/mod.rs @@ -31,3 +31,4 @@ pub use uri::percent_encode_path; mod error; pub use error::parse_error_kind; pub use error::parse_error_response; +pub use error::parse_error_status_code; diff --git a/src/services/azblob/backend.rs b/src/services/azblob/backend.rs index 969f8b8df4c..a41e272a16b 100644 --- a/src/services/azblob/backend.rs +++ b/src/services/azblob/backend.rs @@ -18,7 +18,6 @@ use std::fmt::Debug; use std::fmt::Formatter; use std::fmt::Write; use std::io::Error; -use std::io::ErrorKind; use std::io::Result; use std::mem; use std::sync::Arc; @@ -45,6 +44,7 @@ use crate::http_util::new_http_channel; use crate::http_util::parse_content_length; use crate::http_util::parse_error_kind as parse_http_error_kind; use crate::http_util::parse_error_response; +use crate::http_util::parse_error_status_code; use crate::http_util::parse_etag; use crate::http_util::parse_last_modified; use crate::http_util::percent_encode_path; @@ -326,7 +326,9 @@ impl Accessor for Backend { Ok(()) } _ => { - let err = parse_error_response("create", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("create", args.path(), parse_error_status_code, resp) + .await; warn!("object {} create: {:?}", args.path(), err); Err(err) } @@ -358,7 +360,8 @@ impl Accessor for Backend { Ok(Box::new(resp.into_body())) } _ => { - let err = parse_error_response("read", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("read", args.path(), parse_error_status_code, resp).await; warn!("object {} read: {:?}", args.path(), err); Err(err) } @@ -379,7 +382,7 @@ impl Accessor for Backend { tx, self.client.send_async(req), HashSet::from([StatusCode::CREATED, StatusCode::OK]), - parse_error_kind, + parse_error_status_code, ); Ok(Box::new(bs)) @@ -442,7 +445,8 @@ impl Accessor for Backend { Ok(m) } _ => { - let err = parse_error_response("stat", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("stat", args.path(), parse_error_status_code, resp).await; warn!("object {} stat: {:?}", args.path(), err); Err(err) } @@ -463,7 +467,9 @@ impl Accessor for Backend { Ok(()) } _ => { - let err = parse_error_response("delete", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("delete", args.path(), parse_error_status_code, resp) + .await; warn!("object {} delete: {:?}", args.path(), err); Err(err) } @@ -703,15 +709,3 @@ impl Backend { }) } } - -pub fn parse_error_kind(code: StatusCode) -> ErrorKind { - match code { - StatusCode::NOT_FOUND => ErrorKind::NotFound, - StatusCode::FORBIDDEN => ErrorKind::PermissionDenied, - StatusCode::INTERNAL_SERVER_ERROR - | StatusCode::BAD_GATEWAY - | StatusCode::SERVICE_UNAVAILABLE - | StatusCode::GATEWAY_TIMEOUT => ErrorKind::Interrupted, - _ => ErrorKind::Other, - } -} diff --git a/src/services/azblob/dir_stream.rs b/src/services/azblob/dir_stream.rs index 4d16dd7da0f..36fa932dac1 100644 --- a/src/services/azblob/dir_stream.rs +++ b/src/services/azblob/dir_stream.rs @@ -30,7 +30,7 @@ use super::Backend; use crate::error::other; use crate::error::ObjectError; use crate::http_util::parse_error_response; -use crate::services::azblob::backend::parse_error_kind; +use crate::http_util::parse_error_status_code; use crate::DirEntry; use crate::ObjectMode; @@ -76,9 +76,13 @@ impl futures::Stream for DirStream { let mut resp = backend.list_blobs(&path, &next_marker).await?; if resp.status() != http::StatusCode::OK { - return Err( - parse_error_response("list", &path, parse_error_kind, resp).await - ); + return Err(parse_error_response( + "list", + &path, + parse_error_status_code, + resp, + ) + .await); } let bs = resp diff --git a/src/services/http/backend.rs b/src/services/http/backend.rs index ac462e61fd3..3d5536cc3ea 100644 --- a/src/services/http/backend.rs +++ b/src/services/http/backend.rs @@ -44,6 +44,7 @@ use crate::http_util::parse_content_length; use crate::http_util::parse_content_md5; use crate::http_util::parse_error_kind as parse_http_error_kind; use crate::http_util::parse_error_response; +use crate::http_util::parse_error_status_code; use crate::http_util::parse_etag; use crate::http_util::parse_last_modified; use crate::http_util::percent_encode_path; @@ -322,7 +323,9 @@ impl Accessor for Backend { Ok(()) } _ => { - let err = parse_error_response("create", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("create", args.path(), parse_error_status_code, resp) + .await; warn!("object {} create: {:?}", args.path(), err); Err(err) } @@ -357,7 +360,9 @@ impl Accessor for Backend { Ok(Box::new(resp.into_body())) } - _ => Err(parse_error_response("read", args.path(), parse_error_kind, resp).await), + _ => { + Err(parse_error_response("read", args.path(), parse_error_status_code, resp).await) + } } } @@ -374,7 +379,7 @@ impl Accessor for Backend { tx, self.client.send_async(req), HashSet::from([StatusCode::CREATED, StatusCode::OK]), - parse_error_kind, + parse_error_status_code, ); self.insert_path(&self.get_index_path(args.path())); @@ -441,7 +446,9 @@ impl Accessor for Backend { debug!("object {} stat finished", &p); Ok(m) } - _ => Err(parse_error_response("stat", args.path(), parse_error_kind, resp).await), + _ => { + Err(parse_error_response("stat", args.path(), parse_error_status_code, resp).await) + } } } @@ -458,7 +465,9 @@ impl Accessor for Backend { Ok(()) } _ => { - let err = parse_error_response("delete", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("delete", args.path(), parse_error_status_code, resp) + .await; warn!("object {} delete: {:?}", args.path(), err); Err(err) } @@ -674,18 +683,6 @@ impl futures::Stream for DirStream { } } -fn parse_error_kind(code: StatusCode) -> ErrorKind { - match code { - StatusCode::NOT_FOUND => ErrorKind::NotFound, - StatusCode::FORBIDDEN => ErrorKind::PermissionDenied, - StatusCode::INTERNAL_SERVER_ERROR - | StatusCode::BAD_GATEWAY - | StatusCode::SERVICE_UNAVAILABLE - | StatusCode::GATEWAY_TIMEOUT => ErrorKind::Interrupted, - _ => ErrorKind::Other, - } -} - #[cfg(test)] mod tests { use anyhow::Result; diff --git a/src/services/s3/backend.rs b/src/services/s3/backend.rs index b6dbbed1f0b..d6f69b26695 100644 --- a/src/services/s3/backend.rs +++ b/src/services/s3/backend.rs @@ -47,6 +47,7 @@ use crate::http_util::new_http_channel; use crate::http_util::parse_content_length; use crate::http_util::parse_error_kind as parse_http_error_kind; use crate::http_util::parse_error_response; +use crate::http_util::parse_error_status_code; use crate::http_util::parse_etag; use crate::http_util::parse_last_modified; use crate::http_util::percent_encode_path; @@ -1136,7 +1137,9 @@ impl Accessor for Backend { Ok(()) } _ => { - let err = parse_error_response("create", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("create", args.path(), parse_error_status_code, resp) + .await; warn!("object {} create: {:?}", args.path(), err); Err(err) } @@ -1175,7 +1178,8 @@ impl Accessor for Backend { Ok(Box::new(resp.into_body())) } _ => { - let err = parse_error_response("read", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("read", args.path(), parse_error_status_code, resp).await; warn!("object {} read: {:?}", args.path(), err); Err(err) } @@ -1196,7 +1200,7 @@ impl Accessor for Backend { tx, self.client.send_async(req), HashSet::from([StatusCode::CREATED, StatusCode::OK]), - parse_error_kind, + parse_error_status_code, ); Ok(Box::new(bs)) @@ -1260,7 +1264,8 @@ impl Accessor for Backend { Ok(m) } _ => { - let err = parse_error_response("stat", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("stat", args.path(), parse_error_status_code, resp).await; warn!("object {} stat: {:?}", args.path(), err); Err(err) } @@ -1282,7 +1287,9 @@ impl Accessor for Backend { Ok(()) } _ => { - let err = parse_error_response("delete", args.path(), parse_error_kind, resp).await; + let err = + parse_error_response("delete", args.path(), parse_error_status_code, resp) + .await; warn!("object {} delete: {:?}", args.path(), err); Err(err) } @@ -1596,18 +1603,6 @@ impl Backend { } } -pub(crate) fn parse_error_kind(code: StatusCode) -> ErrorKind { - match code { - StatusCode::NOT_FOUND => ErrorKind::NotFound, - StatusCode::FORBIDDEN => ErrorKind::PermissionDenied, - StatusCode::INTERNAL_SERVER_ERROR - | StatusCode::BAD_GATEWAY - | StatusCode::SERVICE_UNAVAILABLE - | StatusCode::GATEWAY_TIMEOUT => ErrorKind::Interrupted, - _ => ErrorKind::Other, - } -} - #[cfg(test)] mod tests { use itertools::iproduct; diff --git a/src/services/s3/dir_stream.rs b/src/services/s3/dir_stream.rs index d3647d862a8..96a218a321a 100644 --- a/src/services/s3/dir_stream.rs +++ b/src/services/s3/dir_stream.rs @@ -32,7 +32,7 @@ use super::Backend; use crate::error::other; use crate::error::ObjectError; use crate::http_util::parse_error_response; -use crate::services::s3::backend::parse_error_kind; +use crate::http_util::parse_error_status_code; use crate::DirEntry; use crate::ObjectMode; @@ -78,9 +78,13 @@ impl futures::Stream for DirStream { let mut resp = backend.list_objects(&path, &token).await?; if resp.status() != http::StatusCode::OK { - return Err( - parse_error_response("list", &path, parse_error_kind, resp).await - ); + return Err(parse_error_response( + "list", + &path, + parse_error_status_code, + resp, + ) + .await); } let bs = resp.bytes().await.map_err(|e| {