Enum snafu::guide::examples::backtrace::Error [−][src]
Backtraces aren’t yet supported by the stable Rust compiler. SNAFU provides a stable-compatible way of getting backtraces as well as support for the backtrace support in the nightly compiler. By default, backtraces are disabled. It is expected that the final executable adds SNAFU as a dependency and chooses the appropriate feature flag to enable backtraces.
When using SNAFU to define error types, it’s recommended to start
with a Backtrace
field on every leaf error variant (those
without a source
). Backtraces are only captured on
failure. Since backtraces are disabled by default, adding them in
a library does not force any users to pay the price of backtraces
if they are not used; they can be zero cost.
Certain errors are used for flow control. Those don’t need a
backtrace as they don’t represent actual failures. However,
sometimes an error is mostly used for flow control but might
also indicate an error. In those cases, you can use
Option<Backtrace>
to avoid capturing a backtrace unless an
environment variable is set by the end user to provide additional
debugging.
For variants that do have a source, you need to evaluate if the source error provides a backtrace of some kind. If it is another SNAFU error, for example, you can delegate retrieval of the backtrace to the source error. If the source error doesn’t provide its own backtrace, you should capture your own backtrace. This backtrace would not be as useful as one captured by the source error, but it’s as useful as you can get.
When you wish to display the backtrace of an error, you can use
the ErrorCompat::backtrace
method. It’s recommended to always
use this in the fully-qualified form so it will be easy to find
and replace when Rust stabilizes backtraces.
use snafu::ErrorCompat; fn inner_process() -> Result<(), Error> { // Complicated logic } fn main() { if let Err(e) = inner_process() { eprintln!("An error occurred: {}", e); if let Some(bt) = ErrorCompat::backtrace(&e) { eprintln!("{:?}", bt); } } }
Variants
The most common leaf error should always include a backtrace field. .
Fields of UsualCase
backtrace: Backtrace
When an error is expected to be created frequently but the
backtrace is rarely needed, you can wrap it in an
Option
. See
GenerateBacktrace
for instructions on how to access the backtrace in this
case.q.
This error wraps another error that already has a backtrace. Instead of capturing our own, we forward the request for the backtrace to the inner error. This gives a more accurate backtrace.
Fields of SnafuErrorAsSource
source: ConfigFileError
This error wraps another error that does not expose a backtrace. We capture our own backtrace to provide something useful.
Trait Implementations
impl Debug for Error
[src]
impl Display for Error
[src]
impl Error for Error where
Self: Debug + Display,
[src]
Self: Debug + Display,
fn description(&self) -> &str
[src]
fn cause(&self) -> Option<&dyn Error>
[src]
fn source(&self) -> Option<&(dyn Error + 'static)>
[src]
pub fn backtrace(&self) -> Option<&Backtrace>
[src]
impl ErrorCompat for Error
[src]
impl IntoError<Error> for UsualCase where
Error: Error + ErrorCompat,
[src]
Error: Error + ErrorCompat,
impl IntoError<Error> for UsedInTightLoop where
Error: Error + ErrorCompat,
[src]
Error: Error + ErrorCompat,
impl IntoError<Error> for SnafuErrorAsSource where
Error: Error + ErrorCompat,
[src]
Error: Error + ErrorCompat,
type Source = ConfigFileError
The underlying error
fn into_error(self, error: Self::Source) -> Error
[src]
impl IntoError<Error> for SourceErrorDoesNotHaveBacktrace where
Error: Error + ErrorCompat,
[src]
Error: Error + ErrorCompat,
Auto Trait Implementations
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> AsErrorSource for T where
T: 'static + Error,
[src]
T: 'static + Error,
pub fn as_error_source(&Self) -> &(dyn Error + 'static)
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,