alamb commented on code in PR #10186:
URL: https://github.com/apache/datafusion/pull/10186#discussion_r1579402163
##########
datafusion/common/src/error.rs:
##########
@@ -450,6 +396,99 @@ impl DataFusionError {
#[cfg(not(feature = "backtrace"))]
"".to_owned()
}
+
+ fn get_error_name(&self) -> &'static str {
+ match self {
+ DataFusionError::ArrowError(_, _) => "Arrow error: ",
+ #[cfg(feature = "parquet")]
+ DataFusionError::ParquetError(_) => "Parquet error: ",
+ #[cfg(feature = "avro")]
+ DataFusionError::AvroError(_) => "Avro error: ",
+ #[cfg(feature = "object_store")]
+ DataFusionError::ObjectStore(_) => "Object Store error: ",
+ DataFusionError::IoError(_) => "IO error: ",
+ DataFusionError::SQL(_, _) => "SQL error: ",
+ DataFusionError::NotImplemented(_) => "This feature is not
implemented: ",
+ DataFusionError::Internal(_) => "Internal error: ",
+ DataFusionError::Plan(_) => "Error during planning: ",
+ DataFusionError::Configuration(_) => "Invalid or Unsupported
Configuration: ",
+ DataFusionError::SchemaError(_, _) => "Schema error: ",
+ DataFusionError::Execution(_) => "Execution error: ",
+ DataFusionError::ResourcesExhausted(_) => "Resources exhausted: ",
+ DataFusionError::External(_) => "External error: ",
+ DataFusionError::Context(_, _) => "",
+ DataFusionError::Substrait(_) => "Substrait error: ",
+ }
+ }
+
+ fn format_err(&self, f: &mut Formatter) -> std::fmt::Result {
+ let error_name = self.get_error_name();
+ match *self {
+ DataFusionError::ArrowError(ref desc, ref backtrace) => {
+ let backtrace = backtrace.clone().unwrap_or("".to_owned());
+ write!(f, "{error_name}{desc}{backtrace}")
+ }
+ #[cfg(feature = "parquet")]
+ DataFusionError::ParquetError(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ #[cfg(feature = "avro")]
+ DataFusionError::AvroError(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::IoError(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::SQL(ref desc, ref backtrace) => {
+ let backtrace: String =
backtrace.clone().unwrap_or("".to_owned());
+ write!(f, "{error_name}{desc:?}{backtrace}")
+ }
+ DataFusionError::Configuration(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::NotImplemented(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::Internal(ref desc) => {
+ write!(f, "{error_name}{desc}.\nThis was likely caused by a
bug in DataFusion's \
+ code and we would welcome that you file an bug report in
our issue tracker")
+ }
+ DataFusionError::Plan(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::SchemaError(ref desc, ref backtrace) => {
+ let backtrace: &str =
+ &backtrace.as_ref().clone().unwrap_or("".to_owned());
+ write!(f, "{error_name}{desc}{backtrace}")
+ }
+ DataFusionError::Execution(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::ResourcesExhausted(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::External(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ #[cfg(feature = "object_store")]
+ DataFusionError::ObjectStore(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ DataFusionError::Context(ref desc, ref err) => {
+ write!(f, "{error_name}{desc}\ncaused by\n{}", *err)
+ }
+ DataFusionError::Substrait(ref desc) => {
+ write!(f, "{error_name}{desc}")
+ }
+ }
+ }
+
+ /// Strips error description out of the error message
+ /// Error has a format `{error_name}{`error`}
+ /// The method strips the `error_name` from level and outputs 'error``
+ pub fn strip_error_name(self) -> String {
+ self.to_string().replace(self.get_error_name(), "")
Review Comment:
I am not sure exactly what the usecase is, but if seems like if the only
need is:
> Sometimes it is required to fetch the original message without Datafusion
error name.
Rather than trying to fix the message after it has been created, how about
we add a function that just provides the underlying message? Something like
```rust
impl DataFusionError {
/// retrieve the underlying error message for this error, without the
/// type prefix.
///
/// For example, if err.to_string() returns `Error during planning: My
Query Error`
/// this function would return only `My Query Error`
fn message(&self) -> &str { .. }
}
```
And then the impl could be
```rust
impl Display for DataFusionError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match *self {
DataFusionError::ArrowError(ref desc, ref backtrace) => {
let backtrace = backtrace.clone().unwrap_or("".to_owned());
write!(f, "Arrow error: {}{backtrace}", self.message())
}
#[cfg(feature = "parquet")]
DataFusionError::ParquetError(ref desc) => {
write!(f, "Parquet error: {}", message)
}
...
}
```
Maybe ` fn message(&self)` would have to return `Cow<str>` or something to
avoid copying in the external cases....
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]