Skip to content

Commit bbdfbaa

Browse files
committed
strip_backtrace
1 parent 23830f1 commit bbdfbaa

File tree

13 files changed

+86
-66
lines changed

13 files changed

+86
-66
lines changed

datafusion/common/src/column.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ mod tests {
428428
)
429429
.expect_err("should've failed to find field");
430430
let expected = r#"Schema error: No field named z. Valid fields are t1.a, t1.b, t2.c, t2.d, t3.a, t3.b, t3.c, t3.d, t3.e."#;
431-
assert!(err.to_string().starts_with(expected));
431+
assert_eq!(err.strip_backtrace(), expected);
432432

433433
// ambiguous column reference
434434
let col = Column::from_name("a");
@@ -439,7 +439,7 @@ mod tests {
439439
)
440440
.expect_err("should've found ambiguous field");
441441
let expected = "Schema error: Ambiguous reference to unqualified field a";
442-
assert!(err.to_string().starts_with(expected));
442+
assert_eq!(err.strip_backtrace(), expected);
443443

444444
Ok(())
445445
}

datafusion/common/src/dfschema.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,10 @@ mod tests {
811811
let schema = DFSchema::try_from_qualified_schema("t1", &test_schema_1())?;
812812
// lookup with unqualified name "t1.c0"
813813
let err = schema.index_of_column(&col).unwrap_err();
814-
assert!(err.to_string().starts_with(
814+
assert_eq!(
815+
err.strip_backtrace(),
815816
"Schema error: No field named \"t1.c0\". Valid fields are t1.c0, t1.c1."
816-
));
817+
);
817818
Ok(())
818819
}
819820

@@ -830,9 +831,9 @@ mod tests {
830831

831832
// lookup with unqualified name "t1.c0"
832833
let err = schema.index_of_column(&col).unwrap_err();
833-
assert!(
834-
err.to_string().starts_with(
835-
"Schema error: No field named \"t1.c0\". Valid fields are t1.\"CapitalColumn\", t1.\"field.with.period\".")
834+
assert_eq!(
835+
err.strip_backtrace(),
836+
"Schema error: No field named \"t1.c0\". Valid fields are t1.\"CapitalColumn\", t1.\"field.with.period\"."
836837
);
837838
Ok(())
838839
}
@@ -914,9 +915,10 @@ mod tests {
914915
let left = DFSchema::try_from(test_schema_1())?;
915916
let right = DFSchema::try_from(test_schema_1())?;
916917
let join = left.join(&right);
917-
assert!(join.unwrap_err().to_string().starts_with(
918+
assert_eq!(
919+
join.unwrap_err().strip_backtrace(),
918920
"Schema error: Schema contains duplicate unqualified field name c0"
919-
));
921+
);
920922
Ok(())
921923
}
922924

@@ -991,16 +993,12 @@ mod tests {
991993

992994
let col = Column::from_qualified_name("t1.c0");
993995
let err = schema.index_of_column(&col).unwrap_err();
994-
assert!(err
995-
.to_string()
996-
.starts_with("Schema error: No field named t1.c0."));
996+
assert_eq!(err.strip_backtrace(), "Schema error: No field named t1.c0.");
997997

998998
// the same check without qualifier
999999
let col = Column::from_name("c0");
10001000
let err = schema.index_of_column(&col).err().unwrap();
1001-
assert!(err
1002-
.to_string()
1003-
.starts_with("Schema error: No field named c0."));
1001+
assert_eq!(err.strip_backtrace(), "Schema error: No field named c0.");
10041002
}
10051003

10061004
#[test]

datafusion/common/src/error.rs

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,9 @@ impl From<GenericError> for DataFusionError {
276276
}
277277
}
278278

279-
fn get_back_trace() -> String {
280-
let back_trace = Backtrace::capture();
281-
if back_trace.status() == BacktraceStatus::Captured {
282-
return format!("\nback trace: {}", back_trace);
283-
}
284-
285-
"".to_string()
286-
}
287-
288279
impl Display for DataFusionError {
289280
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
290-
let back_trace_desc: String = get_back_trace();
281+
let back_trace_desc: String = DataFusionError::get_back_trace();
291282

292283
match *self {
293284
DataFusionError::ArrowError(ref desc) => {
@@ -423,6 +414,24 @@ impl DataFusionError {
423414
pub fn context(self, description: impl Into<String>) -> Self {
424415
Self::Context(description.into(), Box::new(self))
425416
}
417+
418+
pub fn strip_backtrace(&self) -> String {
419+
self.to_string()
420+
.split("\n\nbacktrace: ")
421+
.collect::<Vec<&str>>()
422+
.first()
423+
.unwrap_or_else(|| &"")
424+
.to_string()
425+
}
426+
427+
fn get_back_trace() -> String {
428+
let back_trace = Backtrace::capture();
429+
if back_trace.status() == BacktraceStatus::Captured {
430+
return format!("\n\nbacktrace: {}", back_trace);
431+
}
432+
433+
"".to_string()
434+
}
426435
}
427436

428437
/// Unwrap an `Option` if possible. Otherwise return an `DataFusionError::Internal`.
@@ -507,17 +516,16 @@ mod test {
507516
#[test]
508517
fn arrow_error_to_datafusion() {
509518
let res = return_arrow_error().unwrap_err();
510-
assert!(res
511-
.to_string()
512-
.starts_with("External error: Error during planning: foo"));
519+
assert_eq!(
520+
res.to_string(),
521+
"External error: Error during planning: foo"
522+
);
513523
}
514524

515525
#[test]
516526
fn datafusion_error_to_arrow() {
517527
let res = return_datafusion_error().unwrap_err();
518-
assert!(res
519-
.to_string()
520-
.starts_with("Arrow error: Schema error: bar"));
528+
assert_eq!(res.strip_backtrace(), "Arrow error: Schema error: bar");
521529
}
522530

523531
#[test]
@@ -580,35 +588,39 @@ mod test {
580588
fn test_make_error_parse_input() {
581589
let res: Result<(), DataFusionError> = plan_err!("Err");
582590
let res = res.unwrap_err();
583-
assert!(res.to_string().starts_with("Error during planning: Err"));
591+
assert_eq!(res.strip_backtrace(), "Error during planning: Err");
584592

585593
let extra1 = "extra1";
586594
let extra2 = "extra2";
587595

588596
let res: Result<(), DataFusionError> = plan_err!("Err {} {}", extra1, extra2);
589597
let res = res.unwrap_err();
590-
assert!(res
591-
.to_string()
592-
.starts_with("Error during planning: Err extra1 extra2"));
598+
assert_eq!(
599+
res.strip_backtrace(),
600+
"Error during planning: Err extra1 extra2"
601+
);
593602

594603
let res: Result<(), DataFusionError> =
595604
plan_err!("Err {:?} {:#?}", extra1, extra2);
596605
let res = res.unwrap_err();
597-
assert!(res
598-
.to_string()
599-
.starts_with("Error during planning: Err \"extra1\" \"extra2\""));
606+
assert_eq!(
607+
res.strip_backtrace(),
608+
"Error during planning: Err \"extra1\" \"extra2\""
609+
);
600610

601611
let res: Result<(), DataFusionError> = plan_err!("Err {extra1} {extra2}");
602612
let res = res.unwrap_err();
603-
assert!(res
604-
.to_string()
605-
.starts_with("Error during planning: Err extra1 extra2"));
613+
assert_eq!(
614+
res.strip_backtrace(),
615+
"Error during planning: Err extra1 extra2"
616+
);
606617

607618
let res: Result<(), DataFusionError> = plan_err!("Err {extra1:?} {extra2:#?}");
608619
let res = res.unwrap_err();
609-
assert!(res
610-
.to_string()
611-
.starts_with("Error during planning: Err \"extra1\" \"extra2\""));
620+
assert_eq!(
621+
res.strip_backtrace(),
622+
"Error during planning: Err \"extra1\" \"extra2\""
623+
);
612624
}
613625

614626
/// Model what happens when implementing SendableRecordBatchStream:
@@ -629,7 +641,9 @@ mod test {
629641
let e = e.find_root();
630642

631643
// DataFusionError does not implement Eq, so we use a string comparison + some cheap "same variant" test instead
632-
assert!(e.to_string().starts_with(&exp.to_string()));
644+
dbg!(e.to_string());
645+
dbg!(exp.to_string());
646+
assert_eq!(e.strip_backtrace(), exp.to_string());
633647
assert_eq!(std::mem::discriminant(e), std::mem::discriminant(&exp),)
634648
}
635649
}

datafusion/common/src/scalar.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,10 +3238,14 @@ mod tests {
32383238
fn scalar_sub_trait_int32_overflow_test() {
32393239
let int_value = ScalarValue::Int32(Some(i32::MAX));
32403240
let int_value_2 = ScalarValue::Int32(Some(i32::MIN));
3241-
let err = int_value.sub_checked(&int_value_2).unwrap_err().to_string();
3242-
assert!(err.starts_with(
3241+
let err = int_value
3242+
.sub_checked(&int_value_2)
3243+
.unwrap_err()
3244+
.strip_backtrace();
3245+
assert_eq!(
3246+
err,
32433247
"Arrow error: Compute error: Overflow happened on: 2147483647 - -2147483648"
3244-
))
3248+
)
32453249
}
32463250

32473251
#[test]
@@ -3257,8 +3261,11 @@ mod tests {
32573261
fn scalar_sub_trait_int64_overflow_test() {
32583262
let int_value = ScalarValue::Int64(Some(i64::MAX));
32593263
let int_value_2 = ScalarValue::Int64(Some(i64::MIN));
3260-
let err = int_value.sub_checked(&int_value_2).unwrap_err().to_string();
3261-
assert!(err.starts_with("Arrow error: Compute error: Overflow happened on: 9223372036854775807 - -9223372036854775808"))
3264+
let err = int_value
3265+
.sub_checked(&int_value_2)
3266+
.unwrap_err()
3267+
.strip_backtrace();
3268+
assert_eq!(err, "Arrow error: Compute error: Overflow happened on: 9223372036854775807 - -9223372036854775808")
32623269
}
32633270

32643271
#[test]

datafusion/core/src/catalog/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ mod tests {
237237

238238
match catalog.register_schema("foo", schema) {
239239
Ok(_) => panic!("unexpected OK"),
240-
Err(e) => assert!(e.to_string().starts_with("This feature is not implemented: Registering new schemas is not supported")),
240+
Err(e) => assert_eq!(e.strip_backtrace(), "This feature is not implemented: Registering new schemas is not supported"),
241241
};
242242
}
243243

datafusion/core/src/dataframe.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ mod tests {
14281428
// try to sort on some value not present in input to distinct
14291429
.sort(vec![col("c2").sort(true, true)])
14301430
.unwrap_err();
1431-
assert!(err.to_string().starts_with("Error during planning: For SELECT DISTINCT, ORDER BY expressions c2 must appear in select list"));
1431+
assert_eq!(err.strip_backtrace(), "Error during planning: For SELECT DISTINCT, ORDER BY expressions c2 must appear in select list");
14321432

14331433
Ok(())
14341434
}
@@ -1486,7 +1486,7 @@ mod tests {
14861486
.join_on(right, JoinType::Inner, [col("c1").eq(col("c1"))])
14871487
.expect_err("join didn't fail check");
14881488
let expected = "Schema error: Ambiguous reference to unqualified field c1";
1489-
assert!(join.to_string().starts_with(expected));
1489+
assert_eq!(join.strip_backtrace(), expected);
14901490

14911491
Ok(())
14921492
}
@@ -1843,7 +1843,7 @@ mod tests {
18431843
.with_column_renamed("c2", "AAA")
18441844
.unwrap_err();
18451845
let expected_err = "Schema error: Ambiguous reference to unqualified field c2";
1846-
assert!(actual_err.to_string().starts_with(expected_err));
1846+
assert_eq!(actual_err.strip_backtrace(), expected_err);
18471847

18481848
Ok(())
18491849
}

datafusion/core/src/datasource/listing/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,7 @@ mod tests {
18621862
)
18631863
.await
18641864
.expect_err("Example should fail!");
1865-
assert!(e.to_string().starts_with("Invalid or Unsupported Configuration: zstd compression requires specifying a level such as zstd(4)"));
1865+
assert_eq!(e.strip_backtrace(), "Invalid or Unsupported Configuration: zstd compression requires specifying a level such as zstd(4)");
18661866

18671867
Ok(())
18681868
}

datafusion/core/src/datasource/physical_plan/csv.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,10 @@ mod tests {
834834

835835
// errors due to https://github.com/apache/arrow-datafusion/issues/4918
836836
let mut it = csv.execute(0, task_ctx)?;
837-
let err = it.next().await.unwrap().unwrap_err().to_string();
838-
assert!(
839-
err.to_string().starts_with(
840-
"Arrow error: Csv error: incorrect number of fields for line 1, expected 14 got 13")
837+
let err = it.next().await.unwrap().unwrap_err().strip_backtrace();
838+
assert_eq!(
839+
err,
840+
"Arrow error: Csv error: incorrect number of fields for line 1, expected 14 got 13"
841841
);
842842
Ok(())
843843
}
@@ -1074,7 +1074,7 @@ mod tests {
10741074
.write_csv(out_dir_url)
10751075
.await
10761076
.expect_err("should fail because input file does not match inferred schema");
1077-
assert!(e.to_string().starts_with("Arrow error: Parser error: Error while parsing value d for column 0 at line 4"));
1077+
assert_eq!(e.strip_backtrace(), "Arrow error: Parser error: Error while parsing value d for column 0 at line 4");
10781078
Ok(())
10791079
}
10801080

datafusion/core/src/datasource/physical_plan/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ mod tests {
792792
.write_json(out_dir_url)
793793
.await
794794
.expect_err("should fail because input file does not match inferred schema");
795-
assert!(e.to_string().starts_with("Arrow error: Parser error: Error while parsing value d for column 0 at line 4"));
795+
assert_eq!(e.strip_backtrace(), "Arrow error: Parser error: Error while parsing value d for column 0 at line 4");
796796
Ok(())
797797
}
798798

datafusion/core/src/datasource/physical_plan/parquet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ mod tests {
935935
.write_parquet(out_dir_url, None)
936936
.await
937937
.expect_err("should fail because input file does not match inferred schema");
938-
assert!(e.to_string().starts_with("Arrow error: Parser error: Error while parsing value d for column 0 at line 4"));
938+
assert_eq!(e.strip_backtrace(), "Arrow error: Parser error: Error while parsing value d for column 0 at line 4");
939939
Ok(())
940940
}
941941

datafusion/core/src/execution/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,9 +2355,10 @@ mod tests {
23552355
let ctx = SessionContext::new();
23562356

23572357
let err = plan_and_collect(&ctx, "SElECT @= X3").await.unwrap_err();
2358-
assert!(err.to_string().starts_with(
2358+
assert_eq!(
2359+
err.strip_backtrace(),
23592360
"Error during planning: variable [\"@=\"] has no type information"
2360-
));
2361+
);
23612362
Ok(())
23622363
}
23632364

datafusion/core/src/physical_plan/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ mod test {
450450
// get the first result, which should be an error
451451
let first_batch = stream.next().await.unwrap();
452452
let first_err = first_batch.unwrap_err();
453-
assert!(first_err.to_string().starts_with("Execution error: Test1"));
453+
assert_eq!(first_err.strip_backtrace(), "Execution error: Test1");
454454

455455
// There should be no more batches produced (should not get the second error)
456456
assert!(stream.next().await.is_none());

datafusion/core/tests/config_from_env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn from_env() {
3636

3737
// for invalid testing
3838
env::set_var(env_key, "abc");
39-
let err = ConfigOptions::from_env().unwrap_err().to_string();
39+
let err = ConfigOptions::from_env().unwrap_err().strip_backtrace();
4040
assert_eq!(err, "Error parsing abc as usize\ncaused by\nExternal error: invalid digit found in string");
4141

4242
env::remove_var(env_key);

0 commit comments

Comments
 (0)