File tree 1 file changed +36
-3
lines changed
packages/located-error/src
1 file changed +36
-3
lines changed Original file line number Diff line number Diff line change 1
- // https://stackoverflow.com/questions/74336993/getting-line-numbers-with-when-using-boxdyn-stderrorerror
2
-
1
+ //! This crate provides a wrapper around an error that includes the location of
2
+ //! the error.
3
+ //!
4
+ //! ```rust
5
+ //! use std::error::Error;
6
+ //! use std::panic::Location;
7
+ //! use std::sync::Arc;
8
+ //! use torrust_tracker_located_error::{Located, LocatedError};
9
+ //!
10
+ //! #[derive(thiserror::Error, Debug)]
11
+ //! enum TestError {
12
+ //! #[error("Test")]
13
+ //! Test,
14
+ //! }
15
+ //!
16
+ //! #[track_caller]
17
+ //! fn get_caller_location() -> Location<'static> {
18
+ //! *Location::caller()
19
+ //! }
20
+ //!
21
+ //! let e = TestError::Test;
22
+ //!
23
+ //! let b: LocatedError<TestError> = Located(e).into();
24
+ //! let l = get_caller_location();
25
+ //!
26
+ //! assert!(b.to_string().contains("Test, src/lib.rs"));
27
+ //! ```
28
+ //!
29
+ //! # Credits
30
+ //!
31
+ //! <https://stackoverflow.com/questions/74336993/getting-line-numbers-with-when-using-boxdyn-stderrorerror>
3
32
use std:: error:: Error ;
4
33
use std:: panic:: Location ;
5
34
use std:: sync:: Arc ;
6
35
36
+ /// A generic wrapper around an error.
37
+ ///
38
+ /// Where `E` is the inner error (source error).
7
39
pub struct Located < E > ( pub E ) ;
8
40
41
+ /// A wrapper around an error that includes the location of the error.
9
42
#[ derive( Debug ) ]
10
43
pub struct LocatedError < ' a , E >
11
44
where
@@ -78,7 +111,7 @@ mod tests {
78
111
use std:: panic:: Location ;
79
112
80
113
use super :: LocatedError ;
81
- use crate :: located_error :: Located ;
114
+ use crate :: Located ;
82
115
83
116
#[ derive( thiserror:: Error , Debug ) ]
84
117
enum TestError {
You can’t perform that action at this time.
0 commit comments