I am running a Rust warp webserver and I need more descriptive error messages. I'd like to print a backtrace or something similar so I can tell where the error started.
I was using the Failure crate, but it is now deprecated so I migrated to thiserror.
Is it possible (without using nightly), to print a backtrace without panicking?
There are ways to get to the backtrace information - but it relies on what are currently "nightly only" APIs. If you're happy to use nightly and stick with
thiserror, here's what you do... (If not then see the ASIDE at the end for other ideas).If you're creating the error from scratch the steps are:
backtrace: Backtracefield to your error type.backtrace::force_captue()If you're creating this error due to another error and want to use its backtrace instead, then just add #[backtrace] to the
sourcefield where you keep the original error.Then to get the backtrace information you can just use the
backtrace()function onstd::Error.An example looks like this:
This outputs something like:
You can see a working version in the playground
ASIDE
If you're not tied to
thiserrorand need to use the stable version of the compiler, you could instead usesnafuwhich has support for backtraces using thebacktracecrate, rather thanstd::backtrace, and so works on stable.There may also be ways to make
thiserrorwork with thebacktracecrate rather thanstd::backtracebut I've not tried that.