crepererum commented on issue #6377:
URL: https://github.com/apache/arrow-rs/issues/6377#issuecomment-2703409054

   Roughly speaking, you just call 
[`Error::source`](https://doc.rust-lang.org/std/error/trait.Error.html#method.source)
 in a loop and add some string handling:
   
   ```rust
   #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
   #[repr(transparent)]
   pub struct DisplaySourceChain<T>(T);
   
   impl<T: Error + 'static> Display for DisplaySourceChain<T> {
       fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
           // walk the source chain and collect error messages
           let mut err_msgs = Vec::new();
           let mut current_err = Some(&self.0 as &(dyn Error + 'static));
           while let Some(err) = current_err {
               let err_msg = err.to_string();
               err_msgs.push(err_msg);
               current_err = err.source();
           }
           // produce output message parts from source error messages
           // message parts are delimited by the substring ": "
           let mut out_parts = Vec::with_capacity(err_msgs.capacity());
           for err_msg in &err_msgs {
               // not very clean but std lib doesn't easily support splitting 
on two substrings
               for err_part in err_msg.split(": ").flat_map(|s| 
s.split("\ncaused by\n")) {
                   if !err_part.is_empty() && !out_parts.contains(&err_part) {
                       out_parts.push(err_part);
                   }
               }
           }
           write!(f, "{}", out_parts.join(": "))?;
           Ok(())
       }
   }
   ```
   
   Probably not the most elegant code, but it works for us.
   
   You can use a similar trick + 
[downcasting](https://doc.rust-lang.org/std/error/trait.Error.html#method.downcast_ref)
 to convert errors into HTTP status codes, in case you're running the code in 
an REST or gRPC API.


-- 
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-unsubscr...@arrow.apache.org

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

Reply via email to