1
1
//! HTTP error types
2
2
3
+ use std:: convert:: TryInto ;
3
4
use std:: error:: Error as StdError ;
4
5
use std:: fmt:: { self , Debug , Display } ;
5
6
6
7
use crate :: StatusCode ;
7
- use std:: convert:: TryInto ;
8
+
9
+ #[ cfg( all( not( backtrace) , feature = "error_eyre" ) ) ]
10
+ use stable_eyre:: BacktraceExt ;
11
+
12
+ #[ cfg( feature = "error_anyhow" ) ]
13
+ use anyhow:: Error as BaseError ;
14
+ #[ cfg( feature = "error_eyre" ) ]
15
+ use eyre:: Report as BaseError ;
8
16
9
17
/// A specialized `Result` type for HTTP operations.
10
18
///
@@ -14,7 +22,7 @@ pub type Result<T> = std::result::Result<T, Error>;
14
22
15
23
/// The error type for HTTP operations.
16
24
pub struct Error {
17
- error : anyhow :: Error ,
25
+ error : BaseError ,
18
26
status : crate :: StatusCode ,
19
27
type_name : Option < & ' static str > ,
20
28
}
@@ -29,7 +37,7 @@ impl Error {
29
37
where
30
38
S : TryInto < StatusCode > ,
31
39
S :: Error : Debug ,
32
- E : Into < anyhow :: Error > ,
40
+ E : Into < BaseError > ,
33
41
{
34
42
Self {
35
43
status : status
@@ -51,7 +59,7 @@ impl Error {
51
59
status : status
52
60
. try_into ( )
53
61
. expect ( "Could not convert into a valid `StatusCode`" ) ,
54
- error : anyhow :: Error :: msg ( msg) ,
62
+ error : BaseError :: msg ( msg) ,
55
63
type_name : None ,
56
64
}
57
65
}
@@ -96,7 +104,7 @@ impl Error {
96
104
/// compiled on a toolchain that does not support backtraces, or
97
105
/// if executed without backtraces enabled with
98
106
/// `RUST_LIB_BACKTRACE=1`.
99
- #[ cfg( backtrace) ]
107
+ #[ cfg( all ( backtrace, feature = "error_anyhow" ) ) ]
100
108
pub fn backtrace ( & self ) -> Option < & std:: backtrace:: Backtrace > {
101
109
let backtrace = self . error . backtrace ( ) ;
102
110
if let std:: backtrace:: BacktraceStatus :: Captured = backtrace. status ( ) {
@@ -106,12 +114,24 @@ impl Error {
106
114
}
107
115
}
108
116
109
- #[ cfg( not( backtrace) ) ]
117
+ #[ cfg( all ( not( backtrace) , feature = "error_anyhow" ) ) ]
110
118
#[ allow( missing_docs) ]
111
119
pub fn backtrace ( & self ) -> Option < ( ) > {
112
120
None
113
121
}
114
122
123
+ #[ cfg( all( backtrace, feature = "error_eyre" ) ) ]
124
+ #[ allow( missing_docs) ]
125
+ pub fn backtrace ( & self ) -> Option < & std:: backtrace:: Backtrace > {
126
+ self . error . backtrace ( )
127
+ }
128
+
129
+ #[ cfg( all( not( backtrace) , feature = "error_eyre" ) ) ]
130
+ #[ allow( missing_docs) ]
131
+ pub fn backtrace ( & self ) -> Option < & backtrace:: Backtrace > {
132
+ self . error . backtrace ( )
133
+ }
134
+
115
135
/// Attempt to downcast the error object to a concrete type.
116
136
pub fn downcast < E > ( self ) -> std:: result:: Result < E , Self >
117
137
where
@@ -158,7 +178,7 @@ impl Debug for Error {
158
178
}
159
179
}
160
180
161
- impl < E : Into < anyhow :: Error > > From < E > for Error {
181
+ impl < E : Into < BaseError > > From < E > for Error {
162
182
fn from ( error : E ) -> Self {
163
183
Self :: new ( StatusCode :: InternalServerError , error)
164
184
}
0 commit comments