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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
use std::panic::Location;
use eyre::WrapErr;
struct LocationHandler {
actual: Option<&'static str>,
expected: &'static str,
}
impl LocationHandler {
fn new(expected: &'static str) -> Self {
LocationHandler {
actual: None,
expected,
}
}
}
impl eyre::EyreHandler for LocationHandler {
fn debug(
&self,
_error: &(dyn std::error::Error + 'static),
_f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
// we assume that if the compiler is new enough to support
// `track_caller` that we will always have `actual` be `Some`, so we can
// safely skip the assertion if the location is `None` which should only
// happen in older rust versions.
if let Some(actual) = self.actual {
assert_eq!(self.expected, actual);
}
Ok(())
}
fn track_caller(&mut self, location: &'static Location<'static>) {
dbg!(location);
self.actual = Some(location.file());
}
}
#[test]
fn test_wrap_err() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
let err = std::fs::read_to_string("totally_fake_path")
.wrap_err("oopsie")
.unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_wrap_err_with() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
let err = std::fs::read_to_string("totally_fake_path")
.wrap_err_with(|| "oopsie")
.unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_context() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
let err = std::fs::read_to_string("totally_fake_path")
.context("oopsie")
.unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_with_context() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
let err = std::fs::read_to_string("totally_fake_path")
.with_context(|| "oopsie")
.unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_option_compat_wrap_err() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
use eyre::ContextCompat;
let err = None::<()>.wrap_err("oopsie").unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_option_compat_wrap_err_with() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
use eyre::ContextCompat;
let err = None::<()>.wrap_err_with(|| "oopsie").unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_option_compat_context() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
use eyre::ContextCompat;
let err = None::<()>.context("oopsie").unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
#[test]
fn test_option_compat_with_context() {
let _ = eyre::set_hook(Box::new(|_e| {
let expected_location = file!();
Box::new(LocationHandler::new(expected_location))
}));
use eyre::ContextCompat;
let err = None::<()>.with_context(|| "oopsie").unwrap_err();
// should panic if the location isn't in our crate
println!("{:?}", err);
}
|