pub enum Error {
    UsualCase {
        backtrace: Backtrace,
    },
    UsedInTightLoop {
        backtrace: Option<Backtrace>,
    },
    SnafuErrorAsSource {
        source: ConfigFileError,
    },
    SourceErrorDoesNotHaveBacktrace {
        source: Error,
        backtrace: Backtrace,
    },
}
Expand description

Rust 1.65 stabilized the std::backtrace::Backtrace type, but there’s not yet a stable abstraction for accessing a backtrace from an arbitrary error value. SNAFU provides a stable-compatible way of accessing backtraces on a SNAFU-created error type. SNAFU also supports environments where backtraces are not available, such as no_std projects.

When defining error types which include backtraces, it’s recommended to start with a Backtrace field on every leaf error variant (those without a source). Backtraces are only captured on failure.

Certain errors are used for flow control. These 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 will 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 there’s a stable way to access 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§

§

UsualCase

Fields

§backtrace: Backtrace

The most common case: leaf errors should always include a backtrace field.

§

UsedInTightLoop

Fields

§backtrace: Option<Backtrace>

When an error is expected to be created frequently but the backtrace is rarely needed, you can wrap it in an Option. See the instructions on how to access the backtrace in this case.

§

SnafuErrorAsSource

Fields

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.

§

SourceErrorDoesNotHaveBacktrace

Fields

§source: Error
§backtrace: Backtrace

This error wraps another error that does not expose a backtrace. We capture our own backtrace to provide something useful.

Trait Implementations§

source§

impl Debug for Error

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Error

source§

fn fmt(&self, __snafu_display_formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Error for Error
where Self: Debug + Display,

source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
source§

fn source(&self) -> Option<&(dyn Error + 'static)>

The lower-level source of this error, if any. Read more
source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more
source§

impl ErrorCompat for Error

source§

fn backtrace(&self) -> Option<&Backtrace>

Returns a Backtrace that may be printed.
source§

fn iter_chain(&self) -> ChainCompat<'_, '_>
where Self: AsErrorSource,

Returns an iterator for traversing the chain of errors, starting with the current error and continuing with recursive calls to Error::source. Read more
source§

impl IntoError<Error> for SnafuErrorAsSourceSnafu

§

type Source = ConfigFileError

The underlying error
source§

fn into_error(self, error: Self::Source) -> Error

Combine the information to produce the error
source§

impl IntoError<Error> for SourceErrorDoesNotHaveBacktraceSnafu

§

type Source = Error

The underlying error
source§

fn into_error(self, error: Self::Source) -> Error

Combine the information to produce the error
source§

impl IntoError<Error> for UsedInTightLoopSnafu

§

type Source = NoneError

The underlying error
source§

fn into_error(self, error: Self::Source) -> Error

Combine the information to produce the error
source§

impl IntoError<Error> for UsualCaseSnafu

§

type Source = NoneError

The underlying error
source§

fn into_error(self, error: Self::Source) -> Error

Combine the information to produce the error

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AsErrorSource for T
where T: Error + 'static,

source§

fn as_error_source(&self) -> &(dyn Error + 'static)

For maximum effectiveness, this needs to be called as a method to benefit from Rust’s automatic dereferencing of method receivers.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.