Skip to main content

format_with_sources

Function format_with_sources 

Source
pub fn format_with_sources(e: &dyn Error) -> String
Expand description

错误格式化工具:把 std::error::Error 的 source 链完整展开为字符串。

存在的原因:tokio_postgres::ErrorDisplay 对 DB 侧错误只会打印 无信息量的占位串 db error,真正的消息文本(如 column "x" of relation "y" already exists、SQLSTATE、约束名)藏在 source() 链里的 postgres::error::DbError。不主动遍历链,日志和错误 字符串就会全部折叠成 db error,无法定位失败原因。

用法:format!("...: {}", format_with_sources(&e)) 或直接 format_with_sources(&e) 得到完整的 e: cause: deeper cause