|
| 1 | +pub fn snafu_error_to_string<E: std::error::Error>(err: &E) -> String { |
| 2 | + let mut result = format!("{err}"); |
| 3 | + let mut source = err.source(); |
| 4 | + while let Some(err) = source { |
| 5 | + result.push_str(format!(": {err}").as_str()); |
| 6 | + source = err.source(); |
| 7 | + } |
| 8 | + |
| 9 | + result |
| 10 | +} |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn test_error_formatting() { |
| 14 | + let err = trino_lb_persistence::Error::RedisError { |
| 15 | + source: trino_lb_persistence::redis::Error::CreateClient { |
| 16 | + source: redis::RedisError::from(std::io::Error::new( |
| 17 | + std::io::ErrorKind::ConnectionRefused, |
| 18 | + "Connection to Redis was refused", |
| 19 | + )), |
| 20 | + }, |
| 21 | + }; |
| 22 | + |
| 23 | + assert_eq!(format!("{err}"), "Redis persistence error"); |
| 24 | + assert_eq!(format!("{err:#}"), "Redis persistence error"); |
| 25 | + assert_eq!( |
| 26 | + format!("{err:?}"), |
| 27 | + "RedisError { source: CreateClient { source: Connection to Redis was refused } }" |
| 28 | + ); |
| 29 | + assert_eq!( |
| 30 | + snafu_error_to_string(&err), |
| 31 | + "Redis persistence error: Failed to create redis client: Connection to Redis was refused: Connection to Redis was refused" |
| 32 | + ); |
| 33 | + assert_eq!( |
| 34 | + format!("{:#}", snafu::Report::from_error(err)), |
| 35 | + "Redis persistence error |
| 36 | +
|
| 37 | +Caused by these errors (recent errors listed first): |
| 38 | + 1: Failed to create redis client |
| 39 | + 2: Connection to Redis was refused |
| 40 | +
|
| 41 | +NOTE: Some redundant information has been removed. Set SNAFU_RAW_ERROR_MESSAGES=1 to disable this behavior. |
| 42 | +" |
| 43 | + ); |
| 44 | +} |
0 commit comments