Skip to content

Commit 270c857

Browse files
jaymellcijothomas
andauthored
http: surface errors based on status code (#1484)
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com>
1 parent d19187d commit 270c857

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

opentelemetry-http/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## vNext
44

5+
### Changed
6+
7+
- **Breaking** Surface non-2xx status codes as errors; change `ResponseExt` trait to return `HttpError` instead of `TraceError`[#1484](https://github.com/open-telemetry/opentelemetry-rust/pull/1484)
8+
9+
510
## v0.10.0
611

712
### Changed

opentelemetry-http/src/lib.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use std::fmt::Debug;
55
pub use bytes::Bytes;
66
#[doc(no_inline)]
77
pub use http::{Request, Response};
8-
use opentelemetry::{
9-
propagation::{Extractor, Injector},
10-
trace::TraceError,
11-
};
8+
use opentelemetry::propagation::{Extractor, Injector};
129

1310
pub struct HeaderInjector<'a>(pub &'a mut http::HeaderMap);
1411

@@ -66,7 +63,7 @@ mod reqwest {
6663
impl HttpClient for reqwest::Client {
6764
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
6865
let request = request.try_into()?;
69-
let mut response = self.execute(request).await?;
66+
let mut response = self.execute(request).await?.error_for_status()?;
7067
let headers = std::mem::take(response.headers_mut());
7168
let mut http_response = Response::builder()
7269
.status(response.status())
@@ -81,7 +78,7 @@ mod reqwest {
8178
impl HttpClient for reqwest::blocking::Client {
8279
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
8380
let request = request.try_into()?;
84-
let mut response = self.execute(request)?;
81+
let mut response = self.execute(request)?.error_for_status()?;
8582
let headers = std::mem::take(response.headers_mut());
8683
let mut http_response = Response::builder()
8784
.status(response.status())
@@ -99,7 +96,7 @@ pub mod surf {
9996

10097
use http::{header::HeaderName, HeaderMap, HeaderValue};
10198

102-
use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};
99+
use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response, ResponseExt};
103100

104101
#[derive(Debug)]
105102
pub struct BasicAuthMiddleware(pub surf::http::auth::BasicAuth);
@@ -148,13 +145,15 @@ pub mod surf {
148145

149146
*http_response.headers_mut() = headers;
150147

151-
Ok(http_response)
148+
Ok(http_response.error_for_status()?)
152149
}
153150
}
154151
}
155152

156153
#[cfg(feature = "isahc")]
157154
mod isahc {
155+
use crate::ResponseExt;
156+
158157
use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};
159158
use isahc::AsyncReadResponseExt;
160159
use std::convert::TryInto as _;
@@ -172,13 +171,15 @@ mod isahc {
172171
.body(bytes.into())?;
173172
*http_response.headers_mut() = headers;
174173

175-
Ok(http_response)
174+
Ok(http_response.error_for_status()?)
176175
}
177176
}
178177
}
179178

180179
#[cfg(any(feature = "hyper", feature = "hyper_tls"))]
181180
pub mod hyper {
181+
use crate::ResponseExt;
182+
182183
use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};
183184
use http::HeaderValue;
184185
use hyper::client::connect::Connect;
@@ -236,19 +237,19 @@ pub mod hyper {
236237
.body(hyper::body::to_bytes(response.into_body()).await?)?;
237238
*http_response.headers_mut() = headers;
238239

239-
Ok(http_response)
240+
Ok(http_response.error_for_status()?)
240241
}
241242
}
242243
}
243244

244245
/// Methods to make working with responses from the [`HttpClient`] trait easier.
245246
pub trait ResponseExt: Sized {
246247
/// 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>;
248249
}
249250

250251
impl<T> ResponseExt for Response<T> {
251-
fn error_for_status(self) -> Result<Self, TraceError> {
252+
fn error_for_status(self) -> Result<Self, HttpError> {
252253
if self.status().is_success() {
253254
Ok(self)
254255
} else {

0 commit comments

Comments
 (0)