Skip to content

Commit b3f2165

Browse files
committed
Use ntex error for http client
1 parent 776ee84 commit b3f2165

25 files changed

+453
-546
lines changed

ntex-tls/examples/webclient.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use ntex::SharedCfg;
2-
use ntex::client::{Client, Connector, error::SendRequestError};
1+
use ntex::client::{Client, Connector, error::ClientError};
2+
use ntex::{SharedCfg, error::Error};
33
use tls_openssl::ssl::{self, SslMethod, SslVerifyMode};
44

55
#[ntex::main]
6-
async fn main() -> Result<(), SendRequestError> {
6+
async fn main() -> Result<(), Error<ClientError>> {
77
env_logger::init();
88
println!("Connecting to openssl webserver: 127.0.0.1:8443");
99

ntex/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
* Move http types to ntex-http
88

9+
* Use ntex_error::Error for http client
10+
911
## [3.6.1] - 2026-03-14
1012

1113
* Re-export more types from ntex-error

ntex/examples/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ntex::client::{Client, error::SendRequestError};
1+
use ntex::{client::Client, client::error::ClientError, error::Error};
22

33
#[ntex::main]
4-
async fn main() -> Result<(), SendRequestError> {
4+
async fn main() -> Result<(), Error<ClientError>> {
55
env_logger::init();
66

77
let client = Client::new().await;

ntex/src/client/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use base64::{Engine, engine::general_purpose::STANDARD as base64};
55
use crate::http::error::HttpError;
66
use crate::http::header::{self, HeaderName, HeaderValue};
77
use crate::service::{Identity, Middleware, Service, ServiceFactory, Stack, boxed};
8-
use crate::{SharedCfg, time::Millis};
8+
use crate::{SharedCfg, error::Error, time::Millis};
99

10-
use super::error::{ClientBuilderError, SendRequestError};
10+
use super::error::{ClientBuilderError, ClientError};
1111
use super::sender::Sender;
1212
use super::service::{ServiceRequest, ServiceResponse};
1313
use super::{Client, ClientConfig, Connector, cfg::ClientConfigInner};
@@ -202,7 +202,7 @@ impl<M> ClientBuilder<M> {
202202
where
203203
T: Into<SharedCfg>,
204204
M: Middleware<Sender, ClientConfig>,
205-
M::Service: Service<ServiceRequest, Response = ServiceResponse, Error = SendRequestError>
205+
M::Service: Service<ServiceRequest, Response = ServiceResponse, Error = Error<ClientError>>
206206
+ 'static,
207207
{
208208
let cfg = cfg.into();

ntex/src/client/connection.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::{fmt, time};
33
use crate::http::body::Body;
44
use crate::http::{Payload, ResponseHead, Version};
55
use crate::io::{IoBoxed, types::HttpProtocol};
6-
use crate::time::Millis;
6+
use crate::{error::Error, time::Millis};
77

8-
use super::{ClientRawRequest, error::SendRequestError, h1proto, h2proto, pool::Acquired};
8+
use super::{ClientRawRequest, error::ClientError, h1proto, h2proto, pool::Acquired};
99

1010
pub(super) enum ConnectionType {
1111
H1(IoBoxed),
@@ -93,7 +93,7 @@ impl Connection {
9393
mut req: ClientRawRequest,
9494
body: Body,
9595
timeout: Millis,
96-
) -> Result<(ResponseHead, Payload), SendRequestError> {
96+
) -> Result<(ResponseHead, Payload), Error<ClientError>> {
9797
match self.io.take().unwrap() {
9898
ConnectionType::H1(io) => {
9999
req.head.version = Version::HTTP_11;

ntex/src/client/connector.rs

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
use std::{error::Error, task::Context, time::Duration};
1+
use std::{error::Error as StdError, task::Context, time::Duration};
22

3-
use crate::connect::{Connect as TcpConnect, Connector as TcpConnector};
3+
use crate::connect::{Connect as TcpConnect, Connector2 as TcpConnector};
4+
use crate::error::{Error, ErrorMapping, with_service};
45
use crate::service::{Service, ServiceCtx, ServiceFactory, apply_fn_factory, boxed};
56
use crate::{SharedCfg, http::Uri, io::IoBoxed, time::Seconds, util::join};
67

7-
use super::{Connect, Connection, error::ConnectError, pool::ConnectionPool};
8+
use super::error::{ClientError, ConnectError};
9+
use super::{Connect, Connection, pool::ConnectionPool};
810

911
#[cfg(feature = "openssl")]
1012
use tls_openssl::ssl::SslConnector as OpensslConnector;
1113

1214
#[cfg(feature = "rustls")]
1315
use tls_rustls::ClientConfig;
1416

15-
type BoxedConnector =
16-
boxed::BoxServiceFactory<SharedCfg, Connect, IoBoxed, ConnectError, Box<dyn Error>>;
17+
type BoxedConnector = boxed::BoxServiceFactory<
18+
SharedCfg,
19+
Connect,
20+
IoBoxed,
21+
Error<ConnectError>,
22+
Box<dyn StdError>,
23+
>;
1724

1825
#[derive(Debug)]
1926
/// Manages http client network connectivity.
@@ -49,8 +56,8 @@ impl Connector {
4956
svc.call(TcpConnect::new(msg.uri).set_addr(msg.addr)).await
5057
})
5158
.map(IoBoxed::from)
52-
.map_err(ConnectError::from)
53-
.map_init_err(|e| Box::new(e) as Box<dyn Error>),
59+
.map_err(|e| e.map(ConnectError::from))
60+
.map_init_err(|e| Box::new(e) as Box<dyn StdError>),
5461
),
5562
secure_svc: None,
5663
conn_lifetime: Duration::from_secs(75),
@@ -96,18 +103,18 @@ impl Connector {
96103
#[cfg(feature = "openssl")]
97104
/// Use openssl connector for secured connections.
98105
pub fn openssl(self, connector: OpensslConnector) -> Self {
99-
use crate::connect::openssl::SslConnector;
106+
use crate::connect::openssl::SslConnector2;
100107

101-
self.secure_connector(SslConnector::new(connector))
108+
self.secure_connector(SslConnector2::new(connector))
102109
}
103110

104111
#[must_use]
105112
#[cfg(feature = "rustls")]
106113
/// Use rustls connector for secured connections.
107114
pub fn rustls(self, connector: ClientConfig) -> Self {
108-
use crate::connect::rustls::TlsConnector;
115+
use crate::connect::rustls::TlsConnector2;
109116

110-
self.secure_connector(TlsConnector::new(connector))
117+
self.secure_connector(TlsConnector2::new(connector))
111118
}
112119

113120
#[must_use]
@@ -147,18 +154,21 @@ impl Connector {
147154
/// Use custom connector to open un-secured connections.
148155
pub fn connector<T>(mut self, connector: T) -> Self
149156
where
150-
T: ServiceFactory<TcpConnect<Uri>, SharedCfg, Error = crate::connect::ConnectError>
151-
+ 'static,
152-
T::InitError: Error,
157+
T: ServiceFactory<
158+
TcpConnect<Uri>,
159+
SharedCfg,
160+
Error = Error<crate::connect::ConnectError>,
161+
> + 'static,
162+
T::InitError: StdError,
153163
IoBoxed: From<T::Response>,
154164
{
155165
self.svc = boxed::factory(
156166
apply_fn_factory(connector, async move |msg: Connect, svc| {
157167
svc.call(TcpConnect::new(msg.uri).set_addr(msg.addr)).await
158168
})
159169
.map(IoBoxed::from)
160-
.map_err(ConnectError::from)
161-
.map_init_err(|e| Box::new(e) as Box<dyn Error>),
170+
.map_err(|e| e.map(ConnectError::from))
171+
.map_init_err(|e| Box::new(e) as Box<dyn StdError>),
162172
);
163173
self
164174
}
@@ -167,28 +177,31 @@ impl Connector {
167177
/// Use custom connector to open secure connections.
168178
pub fn secure_connector<T>(mut self, connector: T) -> Self
169179
where
170-
T: ServiceFactory<TcpConnect<Uri>, SharedCfg, Error = crate::connect::ConnectError>
171-
+ 'static,
172-
T::InitError: Error,
180+
T: ServiceFactory<
181+
TcpConnect<Uri>,
182+
SharedCfg,
183+
Error = Error<crate::connect::ConnectError>,
184+
> + 'static,
185+
T::InitError: StdError,
173186
IoBoxed: From<T::Response>,
174187
{
175188
self.secure_svc = Some(boxed::factory(
176189
apply_fn_factory(connector, async move |msg: Connect, svc| {
177190
svc.call(TcpConnect::new(msg.uri).set_addr(msg.addr)).await
178191
})
179192
.map(IoBoxed::from)
180-
.map_err(ConnectError::from)
181-
.map_init_err(|e| Box::new(e) as Box<dyn Error>),
193+
.map_err(|e| e.map(ConnectError::from))
194+
.map_init_err(|e| Box::new(e) as Box<dyn StdError>),
182195
));
183196
self
184197
}
185198
}
186199

187200
impl ServiceFactory<Connect, SharedCfg> for Connector {
188201
type Response = Connection;
189-
type Error = ConnectError;
202+
type Error = Error<ClientError>;
190203
type Service = ConnectorService;
191-
type InitError = Box<dyn Error>;
204+
type InitError = Box<dyn StdError>;
192205

193206
async fn create(&self, cfg: SharedCfg) -> Result<Self::Service, Self::InitError> {
194207
let ssl_pool = if let Some(ref svc) = self.secure_svc {
@@ -207,39 +220,44 @@ impl ServiceFactory<Connect, SharedCfg> for Connector {
207220
self.conn_lifetime,
208221
self.conn_keep_alive,
209222
self.limit,
210-
cfg,
223+
cfg.clone(),
211224
);
212-
Ok(ConnectorService { tcp_pool, ssl_pool })
225+
Ok(ConnectorService {
226+
cfg,
227+
tcp_pool,
228+
ssl_pool,
229+
})
213230
}
214231
}
215232

216233
/// Manages http client network connectivity.
217234
#[derive(Clone, Debug)]
218235
pub struct ConnectorService {
236+
cfg: SharedCfg,
219237
tcp_pool: ConnectionPool,
220238
ssl_pool: Option<ConnectionPool>,
221239
}
222240

223241
impl Service<Connect> for ConnectorService {
224242
type Response = Connection;
225-
type Error = ConnectError;
243+
type Error = Error<ClientError>;
226244

227245
#[inline]
228246
async fn ready(&self, ctx: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
229247
if let Some(ref ssl_pool) = self.ssl_pool {
230248
let (r1, r2) = join(ctx.ready(&self.tcp_pool), ctx.ready(ssl_pool)).await;
231-
r1?;
232-
r2
249+
r1.into_error()?;
250+
r2.into_error()
233251
} else {
234-
ctx.ready(&self.tcp_pool).await
252+
ctx.ready(&self.tcp_pool).await.into_error()
235253
}
236254
}
237255

238256
#[inline]
239257
fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
240-
self.tcp_pool.poll(cx)?;
258+
self.tcp_pool.poll(cx).into_error()?; //.map_err(ClientError::from)?;
241259
if let Some(ref ssl_pool) = self.ssl_pool {
242-
ssl_pool.poll(cx)?;
260+
ssl_pool.poll(cx).into_error()?; //map_err(ClientError::from)?;
243261
}
244262
Ok(())
245263
}
@@ -256,16 +274,21 @@ impl Service<Connect> for ConnectorService {
256274
req: Connect,
257275
ctx: ServiceCtx<'_, Self>,
258276
) -> Result<Self::Response, Self::Error> {
259-
match req.uri.scheme_str() {
260-
Some("https" | "wss") => {
261-
if let Some(ref conn) = self.ssl_pool {
262-
ctx.call(conn, req).await
263-
} else {
264-
Err(ConnectError::SslIsNotSupported)
277+
with_service(self.cfg.service(), async {
278+
match req.uri.scheme_str() {
279+
Some("https" | "wss") => {
280+
if let Some(ref conn) = self.ssl_pool {
281+
ctx.call(conn, req).await.into_error()
282+
} else {
283+
Err(Error::from(ClientError::from(
284+
ConnectError::SslIsNotSupported,
285+
)))
286+
}
265287
}
288+
_ => ctx.call(&self.tcp_pool, req).await.into_error(),
266289
}
267-
_ => ctx.call(&self.tcp_pool, req).await,
268-
}
290+
})
291+
.await
269292
}
270293
}
271294

0 commit comments

Comments
 (0)