Always had the problem that if I wanted to just log an error, rather than bubble it all the way up to main(), that you wouldn’t get a stacktrace. You could iterate the source chain and plug the stacktrace together yourself, but it’s rather complex code.
Now I realized, you can do this to get a stacktrace:
let error = todo!("Get an error somehow...");
let error = anyhow::anyhow!(error); //converts to an `anyhow::Error`
eprintln!("Error with stacktrace: {error:?}");
For converting to an anyhow::Error, it often also makes sense to use anyhow::Context like so:
use anyhow::Context;
let error = error.context("Deleting file failed.");


I just optimized the context messages and I’m now happy with:
The “0” context is especially important because it tells you which file is read-only. Here is the code for that:
I don’t think something like “failed to clear dependencies using sqlite db ‘{}’” would be helpful. Something like “failed to” or “error when” does not really add any information. Just describe what is happening. Also, the “clear dependencies” would be redundant because that function can handle that part itself (see msg 1).