From fb54263f7cdc20f46d9dfb3ccae8c4fc2fb09ebc Mon Sep 17 00:00:00 2001 From: "zhongyang.wu" Date: Sat, 28 Nov 2020 21:39:50 -0500 Subject: [PATCH] fix: address comments * Remove HttpClientError --- opentelemetry/src/exporter/trace/mod.rs | 100 +++++++----------------- 1 file changed, 28 insertions(+), 72 deletions(-) diff --git a/opentelemetry/src/exporter/trace/mod.rs b/opentelemetry/src/exporter/trace/mod.rs index 0b5bccf15c..5e16062e17 100644 --- a/opentelemetry/src/exporter/trace/mod.rs +++ b/opentelemetry/src/exporter/trace/mod.rs @@ -1,5 +1,6 @@ //! Trace exporters use crate::api::trace::TraceError; +#[cfg(feature = "http")] use crate::exporter::ExportError; use crate::{ sdk, @@ -13,7 +14,7 @@ use serde::{Deserialize, Serialize}; #[cfg(all(feature = "http", feature = "reqwest"))] use std::convert::TryInto; use std::fmt::Debug; -#[cfg(feature = "http")] +#[cfg(all(feature = "surf", feature = "http"))] use std::fmt::{Display, Formatter}; use std::sync::Arc; use std::time::SystemTime; @@ -72,66 +73,38 @@ pub trait HttpClient: Debug + Send + Sync { async fn send(&self, request: Request>) -> ExportResult; } -/// Error when sending http requests visa HttpClient implementation -#[cfg(feature = "http")] -#[cfg_attr(docsrs, doc(cfg(feature = "http")))] -#[derive(Debug)] -pub enum HttpClientError { - /// Errors from reqwest - #[cfg(all(feature = "reqwest", feature = "http"))] - ReqwestError(reqwest::Error), - - /// Errors from surf - #[cfg(all(feature = "surf", feature = "http"))] - SurfError(surf::Error), - - /// Other errors - Other(String), +#[cfg(all(feature = "reqwest", feature = "http"))] +impl ExportError for reqwest::Error { + fn exporter_name(&self) -> &'static str { + "reqwest" + } } -#[cfg(feature = "http")] -#[cfg_attr(docsrs, doc(cfg(feature = "http")))] -impl std::error::Error for HttpClientError {} - -#[cfg(feature = "http")] -#[cfg_attr(docsrs, doc(cfg(feature = "http")))] -impl ExportError for HttpClientError { +#[cfg(all(feature = "surf", feature = "http"))] +impl ExportError for SurfError { fn exporter_name(&self) -> &'static str { - "http client" + "surf" } } -#[cfg(feature = "http")] -#[cfg_attr(docsrs, doc(cfg(feature = "http")))] -impl Display for HttpClientError { +#[cfg(all(feature = "surf", feature = "http"))] +#[derive(Debug)] +struct SurfError(surf::Error); + +#[cfg(all(feature = "surf", feature = "http"))] +impl Display for SurfError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - match self { - #[cfg(all(feature = "reqwest", feature = "http"))] - HttpClientError::ReqwestError(err) => { - write!(f, "error when sending requests using reqwest, {}", err) - } - #[cfg(all(feature = "surf", feature = "http"))] - HttpClientError::SurfError(err) => write!( - f, - "error when sending requests using surf, {}", - err.to_string() - ), - HttpClientError::Other(msg) => write!(f, "{}", msg), - } + write!(f, "{}", self.0.to_string()) } } -#[cfg(all(feature = "reqwest", feature = "http"))] -impl From for HttpClientError { - fn from(err: reqwest::Error) -> Self { - HttpClientError::ReqwestError(err) - } -} +#[cfg(all(feature = "surf", feature = "http"))] +impl std::error::Error for SurfError {} #[cfg(all(feature = "surf", feature = "http"))] -impl From for HttpClientError { +impl From for SurfError { fn from(err: surf::Error) -> Self { - HttpClientError::SurfError(err) + SurfError(err) } } @@ -174,15 +147,9 @@ pub struct SpanData { impl HttpClient for reqwest::Client { async fn send(&self, request: Request>) -> ExportResult { let _result = self - .execute( - request - .try_into() - .map_err::(Into::into)?, - ) - .await - .map_err::(Into::into)? - .error_for_status() - .map_err::(Into::into)?; + .execute(request.try_into()?) + .await? + .error_for_status()?; Ok(()) } } @@ -191,15 +158,7 @@ impl HttpClient for reqwest::Client { #[async_trait] impl HttpClient for reqwest::blocking::Client { async fn send(&self, request: Request>) -> ExportResult { - let _result = self - .execute( - request - .try_into() - .map_err::(Into::into)?, - ) - .map_err::(Into::into)? - .error_for_status() - .map_err::(Into::into)?; + let _result = self.execute(request.try_into()?)?.error_for_status()?; Ok(()) } } @@ -213,20 +172,17 @@ impl HttpClient for surf::Client { .uri .to_string() .parse() - .map_err(|err: surf::http::url::ParseError| HttpClientError::Other(err.to_string()))?; + .map_err(|_err: surf::http::url::ParseError| TraceError::from("error parse url"))?; let req = surf::Request::builder(surf::http::Method::Post, uri) .content_type("application/json") .body(body); - let result = self - .send(req) - .await - .map_err::(Into::into)?; + let result = self.send(req).await.map_err::(Into::into)?; if result.status().is_success() { Ok(()) } else { - Err(HttpClientError::SurfError(surf::Error::from_str( + Err(SurfError(surf::Error::from_str( result.status(), result.status().canonical_reason(), ))