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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! This crate extends [thiserror](https://crates.io/crates/thiserror/) with possibility to add string context to error enums.
//!
//! A typical usage is annotating `io::Error` with a file name, but it can be used in any case where a string annotation of the error is required.
//!
//! # Relation to other similar crates
//! This crate is not as flexible in adding the context as [anyhow](https://crates.io/crates/anyhow) or [snafu](https://crates.io/crates/snafu), but instead it has a neglegible overhead and is much more ergonomic to use. It only serves a single purpose: adding an explanatory string to the error enum generated with `thiserror`. It doesn't work for any error types other than enums and doesn't support any other context types than strings.
//!
//! In contrast to the other crate with similar purpose - [thiserror-context](https://crates.io/crates/thiserror-context), this crate does not obliges the user to create two distinct error types with and without the context. Instead the hidded context variant is added to the error enum itself, which makes this solution more elegant and much easier to maintain.
//!
//! # Usage
//! The usage is very simple:
//! ```should_panic
//! use thiserror::Error;
//! use thiserror_string_context::*;
//!
//! // Annotate your error enum with `string_context` attribute.
//! // This will allow to use `MyError::with_context()` method
//! // to add a string annotation to your errors.
//! // You may add a custom error message where `{0}`
//! // is your original error variant.
//! #[string_context("Custom context message: {0}")]
//! #[derive(Error,Debug)]
//! enum MyError {
//! #[error("Slight underflow happened!")]
//! Underflow,
//! #[error("slight overflow happened!")]
//! Overflow,
//! #[error("too far from correct!")]
//! TooFar,
//! }
//!
//! fn check_number(n: i32) -> Result<(),MyError> {
//! match n {
//! 42 => println!("Correct number!"),
//! 41 => return Err(MyError::Underflow),
//! 43 => return Err(MyError::Overflow),
//! _ => return Err(MyError::TooFar),
//! }
//! Ok(())
//! }
//!
//! fn initiate_error() -> anyhow::Result<()> {
//! // Here we add a context message
//! check_number(41).with_context(|| "Crashing with value 41")?;
//! Ok(())
//! }
//!
//! # fn main() {
//! # initiate_error().unwrap();
//! # }
//! ```
//! This crashes with the following message:
//! ```text
//! Custom context message: Crashing with value 41
//!
//! Caused by:
//! Slight underflow happened!
//! ```
//!
//! # Matching on error enums with context
//! When the context is added to the error enum a hidden variant is added to it, which makes matching on enum variants somewhat tedious. The method `unwrap_context` retuns a tuple where the first element is `Option<String>` containing the context (if there is any) and the second is the enum itself "peeled" from the context. This allows very simple matching:
//! ```ignore
//! if let Err(err) = initiate_error() {
//! // Run different actions on different error variants
//! match err.unwrap_context() {
//! // Different actions could be performed on the same
//! // variant with and without the context
//! (Some(ctx),MyError::Underflow) => {...},
//! (None,MyError::Underflow) => {...},
//! // The context could be ignored
//! (_,MyError::Overflow) => {...},
//! // The wildcard pattern is required
//! _ => {...},
//! }
//! }
//! ```
pub use string_context;