Skip to content

Commit

Permalink
Use anyhow for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus-consoli committed Oct 17, 2023
1 parent 92e9e91 commit ff3d8d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ http-client = [
"async-std/default",
"dep:tokio",
"dep:hyper",
"dep:anyhow",
]

[dependencies]
Expand All @@ -70,6 +71,7 @@ async-trait = { version = "0.1.40", optional = true }
cfg-if = { version = "1.0.0", optional = true }
tokio = { version = "1.33.0", default-features = false, optional = true }
hyper = { version = "0.14.27", default-features = false, features = ["client", "http1", "http2"], optional = true }
anyhow = { version = "1.0.75", optional = true }
thiserror = "1.0.20"

fluvio-test-derive = { path = "async-test-derive", version = "0.1.1", optional = true }
Expand Down
33 changes: 11 additions & 22 deletions src/http_client/async_std_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use std::{
future::Future,
io,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};

use async_rustls::rustls::{client::InvalidDnsNameError, ClientConfig};
use anyhow::{anyhow, Error};
use async_rustls::rustls::ClientConfig;
use async_std::io::{Read, Write};
use hyper::{
client::connect::{Connected, Connection},
Expand All @@ -24,18 +24,6 @@ use crate::{

const DEFAULT_PORT: u16 = 443;

#[derive(Debug, thiserror::Error)]
pub enum ConnectorError {
#[error("unsupported protocol: {0}")]
UnsupportedProtocol(String),
#[error("no host defined: {0:?}")]
NoHost(Uri),
#[error(transparent)]
IoError(#[from] io::Error),
#[error(transparent)]
InvalidDns(#[from] InvalidDnsNameError),
}

#[derive(Clone)]
pub struct CompatConnector(Arc<TlsConnector>);

Expand All @@ -47,7 +35,7 @@ impl CompatConnector {

impl Service<Uri> for CompatConnector {
type Response = TlsStream;
type Error = ConnectorError;
type Error = Error;
type Future =
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

Expand All @@ -61,23 +49,24 @@ impl Service<Uri> for CompatConnector {
let fut = async move {
let host = uri.host().ok_or_else(|| {
let uri = uri.clone();
ConnectorError::NoHost(uri)
anyhow!("no host defined: {uri}")
})?;

match uri.scheme_str() {
Some("https") => {
let port = uri.port_u16().unwrap_or(DEFAULT_PORT);
let tcp_stream = TcpStream::connect((host, port))
.await
.map_err(ConnectorError::IoError)?;
.map_err(|err| anyhow!("{err}"))?;

let stream = connector.connect(host.try_into()?, tcp_stream).await?;
let host = host
.try_into()
.map_err(|err| anyhow!("invalid DNS: {err}"))?;
let stream = connector.connect(host, tcp_stream).await?;
Ok(TlsStream(stream))
}
Some(scheme) => Err(ConnectorError::UnsupportedProtocol(scheme.to_string())),
_ => Err(ConnectorError::UnsupportedProtocol(
"no scheme provided".to_owned(),
)),
Some(scheme) => Err(anyhow!("unsupported protocol: {scheme}")),
_ => Err(anyhow!("no scheme provided")),
}
};

Expand Down

0 comments on commit ff3d8d8

Please sign in to comment.