Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: surface errors based on status code #1484

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions opentelemetry-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## vNext

### Changed

- **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)


## v0.10.0

### Changed
Expand Down
25 changes: 13 additions & 12 deletions opentelemetry-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use std::fmt::Debug;
pub use bytes::Bytes;
#[doc(no_inline)]
pub use http::{Request, Response};
use opentelemetry::{
propagation::{Extractor, Injector},
trace::TraceError,
};
use opentelemetry::propagation::{Extractor, Injector};

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

Expand Down Expand Up @@ -66,7 +63,7 @@ mod reqwest {
impl HttpClient for reqwest::Client {
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
let request = request.try_into()?;
let mut response = self.execute(request).await?;
let mut response = self.execute(request).await?.error_for_status()?;
let headers = std::mem::take(response.headers_mut());
let mut http_response = Response::builder()
.status(response.status())
Expand All @@ -81,7 +78,7 @@ mod reqwest {
impl HttpClient for reqwest::blocking::Client {
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
let request = request.try_into()?;
let mut response = self.execute(request)?;
let mut response = self.execute(request)?.error_for_status()?;
let headers = std::mem::take(response.headers_mut());
let mut http_response = Response::builder()
.status(response.status())
Expand All @@ -99,7 +96,7 @@ pub mod surf {

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

use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};
use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response, ResponseExt};

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

*http_response.headers_mut() = headers;

Ok(http_response)
Ok(http_response.error_for_status()?)
}
}
}

#[cfg(feature = "isahc")]
mod isahc {
use crate::ResponseExt;

use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};
use isahc::AsyncReadResponseExt;
use std::convert::TryInto as _;
Expand All @@ -172,13 +171,15 @@ mod isahc {
.body(bytes.into())?;
*http_response.headers_mut() = headers;

Ok(http_response)
Ok(http_response.error_for_status()?)
}
}
}

#[cfg(any(feature = "hyper", feature = "hyper_tls"))]
pub mod hyper {
use crate::ResponseExt;

use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};
use http::HeaderValue;
use hyper::client::connect::Connect;
Expand Down Expand Up @@ -236,19 +237,19 @@ pub mod hyper {
.body(hyper::body::to_bytes(response.into_body()).await?)?;
*http_response.headers_mut() = headers;

Ok(http_response)
Ok(http_response.error_for_status()?)
}
}
}

/// Methods to make working with responses from the [`HttpClient`] trait easier.
pub trait ResponseExt: Sized {
/// Turn a response into an error if the HTTP status does not indicate success (200 - 299).
fn error_for_status(self) -> Result<Self, TraceError>;
fn error_for_status(self) -> Result<Self, HttpError>;
}

impl<T> ResponseExt for Response<T> {
fn error_for_status(self) -> Result<Self, TraceError> {
fn error_for_status(self) -> Result<Self, HttpError> {
if self.status().is_success() {
Ok(self)
} else {
Expand Down
Loading