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

[PR] Refine `Display` and `Source` implementation for error types [arrow-rs]

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

   # Which issue does this PR close?
   
   Closes #5438.
   
   # Rationale for this change
    
   Check the issue for the motivation. Also update the `source` implementation to make it consistent with the changes on `Display`.
   
   - If there's a prompt ahead of formatting the inner error, provide it as a source error. This is similar to `#[source]` or `#[from]` in `thiserror`.
   - If there's no prompt but we directly forward the method to the inner error, also forward the `source` method. This is similar to `#[transparent]` in `thiserror`.
   
   # 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?
   
   The description and source of `ArrowError` and `FlightError` get updated.
   
   <!--
   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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


-- 
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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),

Review Comment:
   So the use-case for returning `tonic::Status` in source might be if FlightError was itself wrapped inside another error, potentially an opaque one. A user might then want to walk the source chain to find `tonic::Status` in order to get the error code or something



-- 
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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),
+            FlightError::Tonic(inner) => inner.source(),
+            FlightError::ExternalError(source) => Some(&**source),

Review Comment:
   Looks a bit funky here; does the `e.as_ref()` not work?



-- 
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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),

Review Comment:
   In this sense you're right, as a `FlightError` in `Tonic` variant now acts as if it is a `tonic::Status`. To obtain a `tonic::Status`, the developer could directly match the variants of the enum.
   
   As described in the PR body, I made some variants `transparent` majorly because I didn't find appropriate or user-friendly prompts for them. If you believe a message like `"Tonic error: ..."` or `"Arrow error: ..."` is good, I'm okay to update the `source()` implementation as well.



-- 
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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),

Review Comment:
   Should this return inner.source or inner? I think as written this will prevent walking the stack to obtain a tonic::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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),
+            FlightError::Tonic(inner) => inner.source(),

Review Comment:
   ```suggestion
               FlightError::Arrow(inner) => inner,
               FlightError::Tonic(inner) => inner,
   ```



-- 
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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),

Review Comment:
   So the use-case for returning `tonic::Status` in source might be if FlightError was wrapped itself inside another error, potentially an opaque one, a user might want to walk the source chain to find `tonic::Status` in order to get the error code or something



-- 
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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),
+            FlightError::Tonic(inner) => inner.source(),

Review Comment:
   ```suggestion
               FlightError::Arrow(inner) => Some(inner),
               FlightError::Tonic(inner) => Some(inner),
   ```



-- 
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] Refine `Display` and `Source` implementation for error types [arrow-rs]

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


##########
arrow-flight/src/error.rs:
##########
@@ -49,17 +49,24 @@ impl FlightError {
 
 impl std::fmt::Display for FlightError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        // TODO better format / error
-        write!(f, "{self:?}")
+        match self {
+            FlightError::Arrow(inner) => inner.fmt(f),
+            FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
+            FlightError::Tonic(inner) => inner.fmt(f),
+            FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
+            FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
+            FlightError::ExternalError(source) => write!(f, "External error: {}", source),
+        }
     }
 }
 
 impl Error for FlightError {
     fn source(&self) -> Option<&(dyn Error + 'static)> {
-        if let Self::ExternalError(e) = self {
-            Some(e.as_ref())
-        } else {
-            None
+        match self {
+            FlightError::Arrow(inner) => inner.source(),

Review Comment:
   So the use-case for tonic::Status might be if FlightError was wrapped inside another error, a user might want to walk the source chain to find tonic::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