@@ -56,6 +56,18 @@ impl From<StatusCode> for Error {
56
56
}
57
57
}
58
58
59
+ /// Extends the `Response` type with a method to extract error causes when applicable.
60
+ pub trait ResponseExt {
61
+ /// Extract the cause of the unsuccessful response, if any
62
+ fn err_cause ( & self ) -> Option < & ( dyn std:: error:: Error + Send + Sync + ' static ) > ;
63
+ }
64
+
65
+ impl < T > ResponseExt for Response < T > {
66
+ fn err_cause ( & self ) -> Option < & ( dyn std:: error:: Error + Send + Sync + ' static ) > {
67
+ self . extensions ( ) . get ( ) . map ( |Cause ( c) | & * * c)
68
+ }
69
+ }
70
+
59
71
/// Extends the `Result` type with convenient methods for constructing Tide errors.
60
72
pub trait ResultExt < T > : Sized {
61
73
/// Convert to an `EndpointResult`, treating the `Err` case as a client
@@ -77,27 +89,47 @@ pub trait ResultExt<T>: Sized {
77
89
StatusCode : HttpTryFrom < S > ;
78
90
}
79
91
80
- /// Extends the `Response` type with a method to extract error causes when applicable.
81
- pub trait ResponseExt {
82
- /// Extract the cause of the unsuccessful response, if any
83
- fn err_cause ( & self ) -> Option < & ( dyn std:: error:: Error + Send + Sync + ' static ) > ;
92
+ impl < T , E : std:: error:: Error + Send + Sync + ' static > ResultExt < T > for std:: result:: Result < T , E > {
93
+ fn with_err_status < S > ( self , status : S ) -> EndpointResult < T >
94
+ where
95
+ StatusCode : HttpTryFrom < S > ,
96
+ {
97
+ let r = self . map_err ( |e| Box :: new ( e) as Box < dyn std:: error:: Error + Send + Sync > ) ;
98
+ r. with_err_status ( status)
99
+ }
84
100
}
85
101
86
- impl < T > ResponseExt for Response < T > {
87
- fn err_cause ( & self ) -> Option < & ( dyn std:: error:: Error + Send + Sync + ' static ) > {
88
- self . extensions ( ) . get ( ) . map ( |Cause ( c) | & * * c)
102
+ /// Extends the `Result` type using `std::error::Error` trait object as the error type with
103
+ /// convenient methods for constructing Tide errors.
104
+ pub trait ResultDynErrExt < T > : Sized {
105
+ /// Convert to an `EndpointResult`, treating the `Err` case as a client
106
+ /// error (response code 400).
107
+ fn client_err ( self ) -> EndpointResult < T > {
108
+ self . with_err_status ( 400 )
89
109
}
110
+
111
+ /// Convert to an `EndpointResult`, treating the `Err` case as a server
112
+ /// error (response code 500).
113
+ fn server_err ( self ) -> EndpointResult < T > {
114
+ self . with_err_status ( 500 )
115
+ }
116
+
117
+ /// Convert to an `EndpointResult`, wrapping the `Err` case with a custom
118
+ /// response status.
119
+ fn with_err_status < S > ( self , status : S ) -> EndpointResult < T >
120
+ where
121
+ StatusCode : HttpTryFrom < S > ;
90
122
}
91
123
92
- impl < T , E : std :: error :: Error + Send + Sync + ' static > ResultExt < T > for std:: result:: Result < T , E > {
124
+ impl < T > ResultDynErrExt < T > for std:: result:: Result < T , Box < dyn std :: error :: Error + Send + Sync > > {
93
125
fn with_err_status < S > ( self , status : S ) -> EndpointResult < T >
94
126
where
95
127
StatusCode : HttpTryFrom < S > ,
96
128
{
97
129
self . map_err ( |e| Error {
98
130
resp : Response :: builder ( )
99
131
. status ( status)
100
- . extension ( Cause ( Box :: new ( e ) ) )
132
+ . extension ( Cause ( e ) )
101
133
. body ( Body :: empty ( ) )
102
134
. unwrap ( ) ,
103
135
} )
0 commit comments