You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tustvold (via GitHub)" <gi...@apache.org> on 2023/10/10 14:07:27 UTC

[PR] Cleanup client error handling [arrow-rs]

tustvold opened a new pull request, #4915:
URL: https://github.com/apache/arrow-rs/pull/4915

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Relates to #4880 
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   The existing logic was hard to follow, with various different error variants overlayed onto the same structure. This makes these variants explicit, making the code easier to follow, and better able to support acting on error payloads.
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup `object_store::retry` client error handling [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1353228191


##########
object_store/src/client/retry.rs:
##########
@@ -23,46 +23,50 @@ use futures::FutureExt;
 use reqwest::header::LOCATION;
 use reqwest::{Response, StatusCode};
 use snafu::Error as SnafuError;
+use snafu::Snafu;
 use std::time::{Duration, Instant};
 use tracing::info;
 
 /// Retry request error
-#[derive(Debug)]
-pub struct Error {
-    retries: usize,
-    message: String,
-    source: Option<reqwest::Error>,
-    status: Option<StatusCode>,
-}
-
-impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "response error \"{}\", after {} retries",
-            self.message, self.retries
-        )?;
-        if let Some(source) = &self.source {
-            write!(f, ": {source}")?;
-        }
-        Ok(())
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        self.source.as_ref().map(|e| e as _)
-    }
+#[derive(Debug, Snafu)]
+pub enum Error {
+    #[snafu(display("Received redirect without LOCATION, this normally indicates an incorrectly configured region"))]
+    BareRedirect,
+
+    #[snafu(display("Client error with status {status}: {}", body.as_deref().unwrap_or("No Body")))]

Review Comment:
   It's a client error in the sense, the client did something wrong, which the server then complained about.
   
   As per https://docs.rs/http/latest/http/status/struct.StatusCode.html#method.is_client_error



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup client error handling [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1352589030


##########
object_store/src/client/retry.rs:
##########
@@ -198,28 +199,31 @@ impl RetryExt for reqwest::RequestBuilder {
                                 || now.elapsed() > retry_timeout
                                 || !status.is_server_error() {
 
-                                // Get the response message if returned a client error
-                                let message = match status.is_client_error() {
+                                return Err(match status.is_client_error() {
                                     true => match r.text().await {
-                                        Ok(message) if !message.is_empty() => message,
-                                        Ok(_) => "No Body".to_string(),
-                                        Err(e) => format!("error getting response body: {e}")
+                                        Ok(body) => {
+                                            Error::Client {
+                                                body: Some(body).filter(|b| !b.is_empty()),
+                                                status,
+                                            }
+                                        }
+                                        Err(e) => {
+                                            Error::Response {
+                                                retries,
+                                                source: e,
+                                            }
+                                        }
                                     }
-                                    false => status.to_string(),
-                                };
-
-                                return Err(Error{
-                                    message,
-                                    retries,
-                                    status: Some(status),
-                                    source: Some(e),
-                                })
-
+                                    false => Error::Response {
+                                        retries,
+                                        source: e,
+                                    }
+                                });
                             }
 
                             let sleep = backoff.next();
                             retries += 1;
-                            info!("Encountered server error, backing off for {} seconds, retry {} of {}", sleep.as_secs_f32(), retries, max_retries);
+                            info!("Encountered response error, backing off for {} seconds, retry {} of {}", sleep.as_secs_f32(), retries, max_retries);

Review Comment:
   I thought this was less ambiguous, as this branch is for if we received an HTTP response with an error status



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup client error handling [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1352590468


##########
object_store/src/client/retry.rs:
##########
@@ -238,16 +242,14 @@ impl RetryExt for reqwest::RequestBuilder {
                             || now.elapsed() > retry_timeout
                             || !do_retry {
 
-                            return Err(Error{
+                            return Err(Error::Response {
                                 retries,
-                                message: "request error".to_string(),
-                                status: e.status(),
-                                source: Some(e),
+                                source: e,
                             })
                         }
                         let sleep = backoff.next();
                         retries += 1;
-                        info!("Encountered request error ({}) backing off for {} seconds, retry {} of {}", e, sleep.as_secs_f32(), retries, max_retries);
+                        info!("Encountered transport error ({}) backing off for {} seconds, retry {} of {}", e, sleep.as_secs_f32(), retries, max_retries);

Review Comment:
   I wasn't really sure what to call this, but this variant is for if the underlying connection/stream gets aborted without a response being received, so I went with transport to describe this



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup `object_store::retry` client error handling [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1353228191


##########
object_store/src/client/retry.rs:
##########
@@ -23,46 +23,50 @@ use futures::FutureExt;
 use reqwest::header::LOCATION;
 use reqwest::{Response, StatusCode};
 use snafu::Error as SnafuError;
+use snafu::Snafu;
 use std::time::{Duration, Instant};
 use tracing::info;
 
 /// Retry request error
-#[derive(Debug)]
-pub struct Error {
-    retries: usize,
-    message: String,
-    source: Option<reqwest::Error>,
-    status: Option<StatusCode>,
-}
-
-impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "response error \"{}\", after {} retries",
-            self.message, self.retries
-        )?;
-        if let Some(source) = &self.source {
-            write!(f, ": {source}")?;
-        }
-        Ok(())
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        self.source.as_ref().map(|e| e as _)
-    }
+#[derive(Debug, Snafu)]
+pub enum Error {
+    #[snafu(display("Received redirect without LOCATION, this normally indicates an incorrectly configured region"))]
+    BareRedirect,
+
+    #[snafu(display("Client error with status {status}: {}", body.as_deref().unwrap_or("No Body")))]

Review Comment:
   It's a client error in the sense, the client did something wrong, which the server then complained about.
   
   As per https://docs.rs/http/latest/http/status/struct.StatusCode.html#method.is_client_error
   
   Or https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup `object_store::retry` client error handling [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup client error handling [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1352588238


##########
object_store/src/client/retry.rs:
##########
@@ -23,46 +23,50 @@ use futures::FutureExt;
 use reqwest::header::LOCATION;
 use reqwest::{Response, StatusCode};
 use snafu::Error as SnafuError;
+use snafu::Snafu;
 use std::time::{Duration, Instant};
 use tracing::info;
 
 /// Retry request error
-#[derive(Debug)]
-pub struct Error {
-    retries: usize,
-    message: String,
-    source: Option<reqwest::Error>,
-    status: Option<StatusCode>,
-}
-
-impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "response error \"{}\", after {} retries",
-            self.message, self.retries
-        )?;
-        if let Some(source) = &self.source {
-            write!(f, ": {source}")?;
-        }
-        Ok(())
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        self.source.as_ref().map(|e| e as _)
-    }
+#[derive(Debug, Snafu)]
+pub enum Error {
+    #[snafu(display("Received redirect without LOCATION, this normally indicates an incorrectly configured region"))]
+    BareRedirect,
+
+    #[snafu(display("Client error with status {status}: {}", body.as_deref().unwrap_or("No Body")))]
+    Client {
+        status: StatusCode,
+        body: Option<String>,
+    },
+
+    #[snafu(display("Response error after {retries} retries: {source}"))]
+    Response {
+        retries: usize,
+        source: reqwest::Error,
+    },
 }
 
 impl Error {
     /// Returns the status code associated with this error if any
     pub fn status(&self) -> Option<StatusCode> {
-        self.status
+        match self {
+            Error::BareRedirect => None,
+            Error::Client { status, .. } => Some(*status),
+            Error::Response { source, .. } => source.status(),
+        }
+    }
+
+    /// Returns the error body if any
+    pub fn body(&self) -> Option<&str> {

Review Comment:
   Being able to provide this method is what originally motivated this PR



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup `object_store` client error handling [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1353104089


##########
object_store/src/client/retry.rs:
##########
@@ -23,46 +23,50 @@ use futures::FutureExt;
 use reqwest::header::LOCATION;
 use reqwest::{Response, StatusCode};
 use snafu::Error as SnafuError;
+use snafu::Snafu;
 use std::time::{Duration, Instant};
 use tracing::info;
 
 /// Retry request error
-#[derive(Debug)]
-pub struct Error {
-    retries: usize,
-    message: String,
-    source: Option<reqwest::Error>,
-    status: Option<StatusCode>,
-}
-
-impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "response error \"{}\", after {} retries",
-            self.message, self.retries
-        )?;
-        if let Some(source) = &self.source {
-            write!(f, ": {source}")?;
-        }
-        Ok(())
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        self.source.as_ref().map(|e| e as _)
-    }
+#[derive(Debug, Snafu)]
+pub enum Error {
+    #[snafu(display("Received redirect without LOCATION, this normally indicates an incorrectly configured region"))]
+    BareRedirect,
+
+    #[snafu(display("Client error with status {status}: {}", body.as_deref().unwrap_or("No Body")))]
+    Client {
+        status: StatusCode,
+        body: Option<String>,
+    },
+
+    #[snafu(display("Response error after {retries} retries: {source}"))]

Review Comment:
   Isn't this kind of error normally called a `ClientError` or `ConnectionError` as it denotes a problem communicating with the server?



##########
object_store/src/client/retry.rs:
##########
@@ -238,16 +242,14 @@ impl RetryExt for reqwest::RequestBuilder {
                             || now.elapsed() > retry_timeout
                             || !do_retry {
 
-                            return Err(Error{
+                            return Err(Error::Response {
                                 retries,
-                                message: "request error".to_string(),
-                                status: e.status(),
-                                source: Some(e),
+                                source: e,
                             })
                         }
                         let sleep = backoff.next();
                         retries += 1;
-                        info!("Encountered request error ({}) backing off for {} seconds, retry {} of {}", e, sleep.as_secs_f32(), retries, max_retries);
+                        info!("Encountered transport error ({}) backing off for {} seconds, retry {} of {}", e, sleep.as_secs_f32(), retries, max_retries);

Review Comment:
   I think transport error makes sense ✅ 



##########
object_store/src/client/retry.rs:
##########
@@ -238,16 +242,14 @@ impl RetryExt for reqwest::RequestBuilder {
                             || now.elapsed() > retry_timeout
                             || !do_retry {
 
-                            return Err(Error{
+                            return Err(Error::Response {
                                 retries,
-                                message: "request error".to_string(),
-                                status: e.status(),
-                                source: Some(e),
+                                source: e,
                             })
                         }
                         let sleep = backoff.next();
                         retries += 1;
-                        info!("Encountered request error ({}) backing off for {} seconds, retry {} of {}", e, sleep.as_secs_f32(), retries, max_retries);
+                        info!("Encountered transport error ({}) backing off for {} seconds, retry {} of {}", e, sleep.as_secs_f32(), retries, max_retries);

Review Comment:
   I think transport error makes sense ✅ 



##########
object_store/src/client/retry.rs:
##########
@@ -23,46 +23,50 @@ use futures::FutureExt;
 use reqwest::header::LOCATION;
 use reqwest::{Response, StatusCode};
 use snafu::Error as SnafuError;
+use snafu::Snafu;
 use std::time::{Duration, Instant};
 use tracing::info;
 
 /// Retry request error
-#[derive(Debug)]
-pub struct Error {
-    retries: usize,
-    message: String,
-    source: Option<reqwest::Error>,
-    status: Option<StatusCode>,
-}
-
-impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "response error \"{}\", after {} retries",
-            self.message, self.retries
-        )?;
-        if let Some(source) = &self.source {
-            write!(f, ": {source}")?;
-        }
-        Ok(())
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        self.source.as_ref().map(|e| e as _)
-    }
+#[derive(Debug, Snafu)]
+pub enum Error {
+    #[snafu(display("Received redirect without LOCATION, this normally indicates an incorrectly configured region"))]
+    BareRedirect,
+
+    #[snafu(display("Client error with status {status}: {}", body.as_deref().unwrap_or("No Body")))]

Review Comment:
   Isn't this techically a `ServerError` as is a value returned from the server (rather than some error trying to make the connecton?)



##########
object_store/src/client/retry.rs:
##########
@@ -198,28 +199,31 @@ impl RetryExt for reqwest::RequestBuilder {
                                 || now.elapsed() > retry_timeout
                                 || !status.is_server_error() {
 
-                                // Get the response message if returned a client error
-                                let message = match status.is_client_error() {
+                                return Err(match status.is_client_error() {
                                     true => match r.text().await {
-                                        Ok(message) if !message.is_empty() => message,
-                                        Ok(_) => "No Body".to_string(),
-                                        Err(e) => format!("error getting response body: {e}")
+                                        Ok(body) => {
+                                            Error::Client {
+                                                body: Some(body).filter(|b| !b.is_empty()),
+                                                status,
+                                            }
+                                        }
+                                        Err(e) => {
+                                            Error::Response {
+                                                retries,
+                                                source: e,
+                                            }
+                                        }
                                     }
-                                    false => status.to_string(),
-                                };
-
-                                return Err(Error{
-                                    message,
-                                    retries,
-                                    status: Some(status),
-                                    source: Some(e),
-                                })
-
+                                    false => Error::Response {
+                                        retries,
+                                        source: e,
+                                    }
+                                });
                             }
 
                             let sleep = backoff.next();
                             retries += 1;
-                            info!("Encountered server error, backing off for {} seconds, retry {} of {}", sleep.as_secs_f32(), retries, max_retries);
+                            info!("Encountered response error, backing off for {} seconds, retry {} of {}", sleep.as_secs_f32(), retries, max_retries);

Review Comment:
   I actually think `server error` is what I would expect. I would be confused about what a `response error` was



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup `object_store::retry` client error handling [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1353699068


##########
object_store/src/client/retry.rs:
##########
@@ -39,8 +39,8 @@ pub enum Error {
         body: Option<String>,
     },
 
-    #[snafu(display("Response error after {retries} retries: {source}"))]
-    Response {
+    #[snafu(display("Error after {retries} retries: {source}"))]

Review Comment:
   👍 



##########
object_store/src/client/retry.rs:
##########
@@ -39,8 +39,8 @@ pub enum Error {
         body: Option<String>,
     },
 
-    #[snafu(display("Response error after {retries} retries: {source}"))]
-    Response {
+    #[snafu(display("Error after {retries} retries: {source}"))]

Review Comment:
   👍 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cleanup `object_store::retry` client error handling [arrow-rs]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4915:
URL: https://github.com/apache/arrow-rs/pull/4915#discussion_r1353243290


##########
object_store/src/client/retry.rs:
##########
@@ -23,46 +23,50 @@ use futures::FutureExt;
 use reqwest::header::LOCATION;
 use reqwest::{Response, StatusCode};
 use snafu::Error as SnafuError;
+use snafu::Snafu;
 use std::time::{Duration, Instant};
 use tracing::info;
 
 /// Retry request error
-#[derive(Debug)]
-pub struct Error {
-    retries: usize,
-    message: String,
-    source: Option<reqwest::Error>,
-    status: Option<StatusCode>,
-}
-
-impl std::fmt::Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(
-            f,
-            "response error \"{}\", after {} retries",
-            self.message, self.retries
-        )?;
-        if let Some(source) = &self.source {
-            write!(f, ": {source}")?;
-        }
-        Ok(())
-    }
-}
-
-impl std::error::Error for Error {
-    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
-        self.source.as_ref().map(|e| e as _)
-    }
+#[derive(Debug, Snafu)]
+pub enum Error {
+    #[snafu(display("Received redirect without LOCATION, this normally indicates an incorrectly configured region"))]
+    BareRedirect,
+
+    #[snafu(display("Client error with status {status}: {}", body.as_deref().unwrap_or("No Body")))]
+    Client {
+        status: StatusCode,
+        body: Option<String>,
+    },
+
+    #[snafu(display("Response error after {retries} retries: {source}"))]

Review Comment:
   I removed "Response" to avoid confusion



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org