Skip to content

Commit

Permalink
Re-introduce maybe_from logic for error handling (openzfs#420)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Dagnelie <[email protected]>
  • Loading branch information
pcd1193182 authored May 16, 2022
1 parent 542273d commit be3f954
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
29 changes: 19 additions & 10 deletions cmd/zfs_object_agent/zettaobject/src/object_access/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ impl MaybeFrom<HttpError> for GetError {
impl MaybeFrom<HttpError> for PutError {
fn maybe_from(value: HttpError) -> Result<Self, HttpError> {
match value {
HttpError::StatusCode { status, body: _ } if status.is_client_error() => {
Ok(PutError {})
}
HttpError::StatusCode {
status: StatusCode::NOT_FOUND,
body,
} => Ok(PutError::NoSuchContainer(body)),
_ => Err(value),
}
}
Expand All @@ -113,16 +114,24 @@ where
{
fn from(e: azure_core::HttpError) -> Self {
match e {
HttpError::StatusCode { status, body } => {
HttpError::StatusCode { status, body: _ } => {
if status == StatusCode::FORBIDDEN {
return Self::InvalidCredentials;
}
Self::Unknown(
Response::builder()
.status(status)
.body(Bytes::from(body))
.unwrap(),
)
/*
* XXX we need logic here to handle contentful errors that aren't
* specific to E, like credential and validation issues.
*/
match E::maybe_from(e) {
Ok(err) => Self::Service(err),
Err(HttpError::StatusCode { status, body }) => Self::Unknown(
Response::builder()
.status(status)
.body(Bytes::from(body))
.unwrap(),
),
Err(_) => panic!("Type changed during maybe_from"),
}
}
HttpError::Utf8(err) => Self::InternalError(err.to_string()),
/*
Expand Down
8 changes: 6 additions & 2 deletions cmd/zfs_object_agent/zettaobject/src/object_access/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ impl Display for GetError {
impl Error for GetError {}

#[derive(Debug)]
pub struct PutError {}
pub enum PutError {
NoSuchContainer(String),
}

impl Display for PutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("put error")
match self {
PutError::NoSuchContainer(container) => write!(f, "Container not found: {}", container),
}
}
}
impl Error for PutError {}
Expand Down
2 changes: 1 addition & 1 deletion cmd/zfs_object_agent/zettaobject/src/object_access/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl From<GetObjectError> for GetError {
impl From<PutObjectError> for PutError {
fn from(_: PutObjectError) -> Self {
// XXX S3 put errors are always returned as Unknown, at present.
PutError {}
panic!("S3 PutObjectErrors are not used");
}
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/zfs_object_agent/zettaobject/src/test_connectivity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::access_stats::ObjectAccessOpType;
use crate::object_access::OAError;
use crate::object_access::ObjectAccess;
use crate::object_access::ObjectAccessProtocol;
use crate::object_access::PutError;
use crate::object_access::RequestError;

#[derive(Debug, Deserialize)]
Expand All @@ -34,6 +35,9 @@ async fn do_test_connectivity(object_access: &ObjectAccess) -> Result<(), String
)
.await
{
Err(OAError::RequestError(RequestError::Service(e @ PutError::NoSuchContainer(_)))) => {
Err(format!("Connectivity test failed: {}", e))
}
Err(OAError::RequestError(RequestError::Unknown(response))) => {
/*
* The Byte-Order-Mark (or BOM), is a special marker added at the very beginning of
Expand All @@ -59,10 +63,6 @@ async fn do_test_connectivity(object_access: &ObjectAccess) -> Result<(), String
Err(OAError::TimeoutError(_)) => {
Err("Connectivity test failed with a timeout.".to_string())
}
Err(OAError::RequestError(RequestError::Service(err))) => Err(format!(
"Connectivity test failed due to a service error: {}",
err
)),
Err(OAError::RequestError(RequestError::Credentials(err))) => Err(format!(
"Connectivity test failed due to a credentials error: {}",
err
Expand Down

0 comments on commit be3f954

Please sign in to comment.