Skip to content

Commit 013a858

Browse files
Ian LaiIan Lai
Ian Lai
authored and
Ian Lai
committed
feat: simplify error handling by using string messages directly in external error macros
1 parent cd2335e commit 013a858

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

datafusion-cli/src/print_options.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,7 @@ impl PrintOptions {
143143
format_options: &FormatOptions,
144144
) -> Result<()> {
145145
if self.format == PrintFormat::Table {
146-
let err = std::io::Error::new(
147-
std::io::ErrorKind::Other,
148-
"PrintFormat::Table is not implemented",
149-
);
150-
return external_err!(err);
146+
return external_err!("PrintFormat::Table is not implemented");
151147
};
152148

153149
let stdout = std::io::stdout();

datafusion/common/src/error.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,20 @@ impl DataFusionError {
671671
queue.push_back(self);
672672
ErrorIterator { queue }
673673
}
674+
675+
/// Converts input to a [`GenericError`],
676+
/// handling both regular values and pre-boxed errors to avoid double-boxing.
677+
///
678+
/// The input must implement [`Into<GenericError>`], which includes:
679+
/// [`Error`] types (implementing `std::error::Error + Send + Sync + 'static`)
680+
/// [`String`] and &str (automatically converted to errors)
681+
/// [`Box<dyn Error + Send + Sync>`] (passed through without additional boxing)
682+
pub fn box_error_if_needed<E>(err: E) -> GenericError
683+
where
684+
E: Into<GenericError>,
685+
{
686+
err.into()
687+
}
674688
}
675689

676690
/// A builder for [`DataFusionError`]
@@ -922,9 +936,10 @@ macro_rules! schema_err {
922936
// Exposes a macro to create `DataFusionError::External` with optional backtrace
923937
#[macro_export]
924938
macro_rules! external_datafusion_err {
925-
($ERR:expr $(; diagnostic = $DIAG:expr)?) => {{
939+
($CONVERTIBLE_TO_ERR:expr $(; diagnostic = $DIAG:expr)?) => {{
940+
let boxed_err = $crate::DataFusionError::box_error_if_needed($CONVERTIBLE_TO_ERR);
926941
let err = $crate::error::DataFusionError::External(
927-
Box::new($ERR),
942+
boxed_err,
928943
Some($crate::error::DataFusionError::get_back_trace())
929944
);
930945
$(

datafusion/datasource/src/url.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ impl ListingTableUrl {
128128
};
129129

130130
let url = url_from_filesystem_path(path).ok_or_else(|| {
131-
let err = std::io::Error::new(
132-
std::io::ErrorKind::Other,
133-
format!("Failed to convert path to URL: {path}"),
134-
);
135-
external_datafusion_err!(err)
131+
external_datafusion_err!(format!("Failed to convert path to URL: {path}"))
136132
})?;
137133

138134
Self::try_new(url, glob)

datafusion/sqllogictest/bin/sqllogictests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ async fn run_file_in_runner<D: AsyncDB, M: MakeConnection<Conn = D>>(
271271
}
272272
msg.push_str(&format!("{}. {err}\n\n", i + 1));
273273
}
274-
let err = std::io::Error::new(std::io::ErrorKind::Other, msg);
275-
return external_err!(err);
274+
return external_err!(msg);
276275
}
277276

278277
Ok(())

0 commit comments

Comments
 (0)