With help from this post I am able to track which ? in a function caused the function to return an error. This is wonderful!
thiserror helps reduce all the boilerplate in producing errors, but the only way I know how to have this feature with a thiserror::Error is to manually impl From. Unless someone else knows a way where the library already provides this functionality for me?
Here is an example
#[derive(Debug, thiserror::Error)]
pub enum BoardsExportError {
#[error("{0}")]
Xlsxwriter(String),
#[error(transparent)]
Io(#[from] std::io::Error), // doesn't have loc info
#[error(transparent)]
Other(#[from] DynError), // doesn't have loc info
}
/// manual impl to provide loc info
impl From<xlsxwriter::XlsxError> for BoardsExportError {
#[track_caller]
fn from(e: xlsxwriter::XlsxError) -> Self {
let loc = std::panic::Location::caller();
Self::Xlsxwriter(format!(
"{e}, {}:{}:{}",
loc.file(),
loc.line(),
loc.column()
))
}
}
Does it need to be a std::backtrace::Backtrace, or could you use a backtrace::Backtrace?
The #[error("...")] thing uses the same syntax as format!(), so you would need to use somethnig like #[error("{source} at {}", get_function_name(backtrace))].
std::backtrace isn't enough, unfortunately. Backtrace support in thiserror also uses other nightly features which cause compiler errors if you try to add a Backtrace to your error: Rust Playground