@@ -5,10 +5,7 @@ use std::fmt::Debug;
5
5
pub use bytes:: Bytes ;
6
6
#[ doc( no_inline) ]
7
7
pub use http:: { Request , Response } ;
8
- use opentelemetry:: {
9
- propagation:: { Extractor , Injector } ,
10
- trace:: TraceError ,
11
- } ;
8
+ use opentelemetry:: propagation:: { Extractor , Injector } ;
12
9
13
10
pub struct HeaderInjector < ' a > ( pub & ' a mut http:: HeaderMap ) ;
14
11
@@ -66,7 +63,7 @@ mod reqwest {
66
63
impl HttpClient for reqwest:: Client {
67
64
async fn send ( & self , request : Request < Vec < u8 > > ) -> Result < Response < Bytes > , HttpError > {
68
65
let request = request. try_into ( ) ?;
69
- let mut response = self . execute ( request) . await ?;
66
+ let mut response = self . execute ( request) . await ?. error_for_status ( ) ? ;
70
67
let headers = std:: mem:: take ( response. headers_mut ( ) ) ;
71
68
let mut http_response = Response :: builder ( )
72
69
. status ( response. status ( ) )
@@ -81,7 +78,7 @@ mod reqwest {
81
78
impl HttpClient for reqwest:: blocking:: Client {
82
79
async fn send ( & self , request : Request < Vec < u8 > > ) -> Result < Response < Bytes > , HttpError > {
83
80
let request = request. try_into ( ) ?;
84
- let mut response = self . execute ( request) ?;
81
+ let mut response = self . execute ( request) ?. error_for_status ( ) ? ;
85
82
let headers = std:: mem:: take ( response. headers_mut ( ) ) ;
86
83
let mut http_response = Response :: builder ( )
87
84
. status ( response. status ( ) )
@@ -99,7 +96,7 @@ pub mod surf {
99
96
100
97
use http:: { header:: HeaderName , HeaderMap , HeaderValue } ;
101
98
102
- use super :: { async_trait, Bytes , HttpClient , HttpError , Request , Response } ;
99
+ use super :: { async_trait, Bytes , HttpClient , HttpError , Request , Response , ResponseExt } ;
103
100
104
101
#[ derive( Debug ) ]
105
102
pub struct BasicAuthMiddleware ( pub surf:: http:: auth:: BasicAuth ) ;
@@ -148,13 +145,15 @@ pub mod surf {
148
145
149
146
* http_response. headers_mut ( ) = headers;
150
147
151
- Ok ( http_response)
148
+ Ok ( http_response. error_for_status ( ) ? )
152
149
}
153
150
}
154
151
}
155
152
156
153
#[ cfg( feature = "isahc" ) ]
157
154
mod isahc {
155
+ use crate :: ResponseExt ;
156
+
158
157
use super :: { async_trait, Bytes , HttpClient , HttpError , Request , Response } ;
159
158
use isahc:: AsyncReadResponseExt ;
160
159
use std:: convert:: TryInto as _;
@@ -172,13 +171,15 @@ mod isahc {
172
171
. body ( bytes. into ( ) ) ?;
173
172
* http_response. headers_mut ( ) = headers;
174
173
175
- Ok ( http_response)
174
+ Ok ( http_response. error_for_status ( ) ? )
176
175
}
177
176
}
178
177
}
179
178
180
179
#[ cfg( any( feature = "hyper" , feature = "hyper_tls" ) ) ]
181
180
pub mod hyper {
181
+ use crate :: ResponseExt ;
182
+
182
183
use super :: { async_trait, Bytes , HttpClient , HttpError , Request , Response } ;
183
184
use http:: HeaderValue ;
184
185
use hyper:: client:: connect:: Connect ;
@@ -236,19 +237,19 @@ pub mod hyper {
236
237
. body ( hyper:: body:: to_bytes ( response. into_body ( ) ) . await ?) ?;
237
238
* http_response. headers_mut ( ) = headers;
238
239
239
- Ok ( http_response)
240
+ Ok ( http_response. error_for_status ( ) ? )
240
241
}
241
242
}
242
243
}
243
244
244
245
/// Methods to make working with responses from the [`HttpClient`] trait easier.
245
246
pub trait ResponseExt : Sized {
246
247
/// Turn a response into an error if the HTTP status does not indicate success (200 - 299).
247
- fn error_for_status ( self ) -> Result < Self , TraceError > ;
248
+ fn error_for_status ( self ) -> Result < Self , HttpError > ;
248
249
}
249
250
250
251
impl < T > ResponseExt for Response < T > {
251
- fn error_for_status ( self ) -> Result < Self , TraceError > {
252
+ fn error_for_status ( self ) -> Result < Self , HttpError > {
252
253
if self . status ( ) . is_success ( ) {
253
254
Ok ( self )
254
255
} else {
0 commit comments