1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! Exposing complete backtraces to the location of the error.
//!
//! Start by looking at the error type [`Error`].
use crate::;
/// 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::guide::examples::backtrace::*;
/// use snafu::ErrorCompat;
///
/// fn inner_process() -> Result<(), Error> {
/// // Complicated logic
/// # UsualCaseSnafu.fail()
/// }
///
/// fn main() {
/// if let Err(e) = inner_process() {
/// eprintln!("An error occurred: {}", e);
/// if let Some(bt) = ErrorCompat::backtrace(&e) {
/// eprintln!("{:?}", bt);
/// }
/// }
/// }
/// ```
// This line is only needed to generate documentation; it is not
// needed in most cases:
/// This is a placeholder example and can be ignored.