11//! HTTP error types
22
3+ use std:: convert:: TryInto ;
34use std:: error:: Error as StdError ;
45use std:: fmt:: { self , Debug , Display } ;
56
67use 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 ;
816
917/// A specialized `Result` type for HTTP operations.
1018///
@@ -14,7 +22,7 @@ pub type Result<T> = std::result::Result<T, Error>;
1422
1523/// The error type for HTTP operations.
1624pub struct Error {
17- error : anyhow :: Error ,
25+ error : BaseError ,
1826 status : crate :: StatusCode ,
1927 type_name : Option < & ' static str > ,
2028}
@@ -29,7 +37,7 @@ impl Error {
2937 where
3038 S : TryInto < StatusCode > ,
3139 S :: Error : Debug ,
32- E : Into < anyhow :: Error > ,
40+ E : Into < BaseError > ,
3341 {
3442 Self {
3543 status : status
@@ -51,7 +59,7 @@ impl Error {
5159 status : status
5260 . try_into ( )
5361 . expect ( "Could not convert into a valid `StatusCode`" ) ,
54- error : anyhow :: Error :: msg ( msg) ,
62+ error : BaseError :: msg ( msg) ,
5563 type_name : None ,
5664 }
5765 }
@@ -96,7 +104,7 @@ impl Error {
96104 /// compiled on a toolchain that does not support backtraces, or
97105 /// if executed without backtraces enabled with
98106 /// `RUST_LIB_BACKTRACE=1`.
99- #[ cfg( backtrace) ]
107+ #[ cfg( all ( backtrace, feature = "error_anyhow" ) ) ]
100108 pub fn backtrace ( & self ) -> Option < & std:: backtrace:: Backtrace > {
101109 let backtrace = self . error . backtrace ( ) ;
102110 if let std:: backtrace:: BacktraceStatus :: Captured = backtrace. status ( ) {
@@ -106,12 +114,24 @@ impl Error {
106114 }
107115 }
108116
109- #[ cfg( not( backtrace) ) ]
117+ #[ cfg( all ( not( backtrace) , feature = "error_anyhow" ) ) ]
110118 #[ allow( missing_docs) ]
111119 pub fn backtrace ( & self ) -> Option < ( ) > {
112120 None
113121 }
114122
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+
115135 /// Attempt to downcast the error object to a concrete type.
116136 pub fn downcast < E > ( self ) -> std:: result:: Result < E , Self >
117137 where
@@ -158,7 +178,7 @@ impl Debug for Error {
158178 }
159179}
160180
161- impl < E : Into < anyhow :: Error > > From < E > for Error {
181+ impl < E : Into < BaseError > > From < E > for Error {
162182 fn from ( error : E ) -> Self {
163183 Self :: new ( StatusCode :: InternalServerError , error)
164184 }
0 commit comments