Skip to content

Commit fd2ba29

Browse files
committed
Use ntex error for http client
1 parent 285d174 commit fd2ba29

27 files changed

+420
-516
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ntex-service = "4.6.0"
6868
ntex-tls = "3.4.0"
6969
ntex-macros = "3.2.0"
7070
ntex-util = "3.4.0"
71-
ntex-h2 = "3.8.0"
71+
ntex-h2 = "3.8.1"
7272

7373
ntex-polling = "3.10.0"
7474
ntex-io-uring = "0.7.120"

ntex-error/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl ResultType {
4545
#[allow(deprecated)]
4646
pub const fn as_str(&self) -> &'static str {
4747
match self {
48-
ErrorType::Success => "Success",
48+
ResultType::Success => "Success",
4949
ResultType::ClientError | ResultType::Client => "ClientError",
5050
ResultType::ServiceError | ResultType::Service => "ServiceError",
5151
}

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes
22

3+
4+
## [3.7.0] - 2026-03-xx
5+
6+
* Use ntex_error::Error for http client
7+
38
## [3.6.1] - 2026-03-14
49

510
* 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::http::{Payload, ResponseHead, Version};
55
use crate::io::{IoBoxed, types::HttpProtocol};
66
use crate::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), 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: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
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};
44
use crate::service::{Service, ServiceCtx, ServiceFactory, apply_fn_factory, boxed};
5-
use crate::{SharedCfg, http::Uri, io::IoBoxed, time::Seconds, util::join};
5+
use crate::{SharedCfg, error::Error, http::Uri, io::IoBoxed, time::Seconds, util::join};
66

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

910
#[cfg(feature = "openssl")]
1011
use tls_openssl::ssl::SslConnector as OpensslConnector;
1112

1213
#[cfg(feature = "rustls")]
1314
use tls_rustls::ClientConfig;
1415

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

1824
#[derive(Debug)]
1925
/// Manages http client network connectivity.
@@ -49,8 +55,8 @@ impl Connector {
4955
svc.call(TcpConnect::new(msg.uri).set_addr(msg.addr)).await
5056
})
5157
.map(IoBoxed::from)
52-
.map_err(ConnectError::from)
53-
.map_init_err(|e| Box::new(e) as Box<dyn Error>),
58+
.map_err(|e| e.map(ConnectError::from))
59+
.map_init_err(|e| Box::new(e) as Box<dyn StdError>),
5460
),
5561
secure_svc: None,
5662
conn_lifetime: Duration::from_secs(75),
@@ -96,18 +102,18 @@ impl Connector {
96102
#[cfg(feature = "openssl")]
97103
/// Use openssl connector for secured connections.
98104
pub fn openssl(self, connector: OpensslConnector) -> Self {
99-
use crate::connect::openssl::SslConnector;
105+
use crate::connect::openssl::SslConnector2;
100106

101-
self.secure_connector(SslConnector::new(connector))
107+
self.secure_connector(SslConnector2::new(connector))
102108
}
103109

104110
#[must_use]
105111
#[cfg(feature = "rustls")]
106112
/// Use rustls connector for secured connections.
107113
pub fn rustls(self, connector: ClientConfig) -> Self {
108-
use crate::connect::rustls::TlsConnector;
114+
use crate::connect::rustls::TlsConnector2;
109115

110-
self.secure_connector(TlsConnector::new(connector))
116+
self.secure_connector(TlsConnector2::new(connector))
111117
}
112118

113119
#[must_use]
@@ -147,18 +153,21 @@ impl Connector {
147153
/// Use custom connector to open un-secured connections.
148154
pub fn connector<T>(mut self, connector: T) -> Self
149155
where
150-
T: ServiceFactory<TcpConnect<Uri>, SharedCfg, Error = crate::connect::ConnectError>
151-
+ 'static,
152-
T::InitError: Error,
156+
T: ServiceFactory<
157+
TcpConnect<Uri>,
158+
SharedCfg,
159+
Error = Error<crate::connect::ConnectError>,
160+
> + 'static,
161+
T::InitError: StdError,
153162
IoBoxed: From<T::Response>,
154163
{
155164
self.svc = boxed::factory(
156165
apply_fn_factory(connector, async move |msg: Connect, svc| {
157166
svc.call(TcpConnect::new(msg.uri).set_addr(msg.addr)).await
158167
})
159168
.map(IoBoxed::from)
160-
.map_err(ConnectError::from)
161-
.map_init_err(|e| Box::new(e) as Box<dyn Error>),
169+
.map_err(|e| e.map(ConnectError::from))
170+
.map_init_err(|e| Box::new(e) as Box<dyn StdError>),
162171
);
163172
self
164173
}
@@ -167,28 +176,31 @@ impl Connector {
167176
/// Use custom connector to open secure connections.
168177
pub fn secure_connector<T>(mut self, connector: T) -> Self
169178
where
170-
T: ServiceFactory<TcpConnect<Uri>, SharedCfg, Error = crate::connect::ConnectError>
171-
+ 'static,
172-
T::InitError: Error,
179+
T: ServiceFactory<
180+
TcpConnect<Uri>,
181+
SharedCfg,
182+
Error = Error<crate::connect::ConnectError>,
183+
> + 'static,
184+
T::InitError: StdError,
173185
IoBoxed: From<T::Response>,
174186
{
175187
self.secure_svc = Some(boxed::factory(
176188
apply_fn_factory(connector, async move |msg: Connect, svc| {
177189
svc.call(TcpConnect::new(msg.uri).set_addr(msg.addr)).await
178190
})
179191
.map(IoBoxed::from)
180-
.map_err(ConnectError::from)
181-
.map_init_err(|e| Box::new(e) as Box<dyn Error>),
192+
.map_err(|e| e.map(ConnectError::from))
193+
.map_init_err(|e| Box::new(e) as Box<dyn StdError>),
182194
));
183195
self
184196
}
185197
}
186198

187199
impl ServiceFactory<Connect, SharedCfg> for Connector {
188200
type Response = Connection;
189-
type Error = ConnectError;
201+
type Error = Error<ClientError>;
190202
type Service = ConnectorService;
191-
type InitError = Box<dyn Error>;
203+
type InitError = Box<dyn StdError>;
192204

193205
async fn create(&self, cfg: SharedCfg) -> Result<Self::Service, Self::InitError> {
194206
let ssl_pool = if let Some(ref svc) = self.secure_svc {
@@ -207,39 +219,44 @@ impl ServiceFactory<Connect, SharedCfg> for Connector {
207219
self.conn_lifetime,
208220
self.conn_keep_alive,
209221
self.limit,
210-
cfg,
222+
cfg.clone(),
211223
);
212-
Ok(ConnectorService { tcp_pool, ssl_pool })
224+
Ok(ConnectorService {
225+
cfg,
226+
tcp_pool,
227+
ssl_pool,
228+
})
213229
}
214230
}
215231

216232
/// Manages http client network connectivity.
217233
#[derive(Clone, Debug)]
218234
pub struct ConnectorService {
235+
cfg: SharedCfg,
219236
tcp_pool: ConnectionPool,
220237
ssl_pool: Option<ConnectionPool>,
221238
}
222239

223240
impl Service<Connect> for ConnectorService {
224241
type Response = Connection;
225-
type Error = ConnectError;
242+
type Error = Error<ClientError>;
226243

227244
#[inline]
228245
async fn ready(&self, ctx: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
229246
if let Some(ref ssl_pool) = self.ssl_pool {
230247
let (r1, r2) = join(ctx.ready(&self.tcp_pool), ctx.ready(ssl_pool)).await;
231-
r1?;
232-
r2
248+
r1.map_err(ClientError::from)?;
249+
Ok(r2.map_err(ClientError::from)?)
233250
} else {
234-
ctx.ready(&self.tcp_pool).await
251+
Ok(ctx.ready(&self.tcp_pool).await.map_err(ClientError::from)?)
235252
}
236253
}
237254

238255
#[inline]
239256
fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
240-
self.tcp_pool.poll(cx)?;
257+
self.tcp_pool.poll(cx).map_err(ClientError::from)?;
241258
if let Some(ref ssl_pool) = self.ssl_pool {
242-
ssl_pool.poll(cx)?;
259+
ssl_pool.poll(cx).map_err(ClientError::from)?;
243260
}
244261
Ok(())
245262
}
@@ -259,13 +276,20 @@ impl Service<Connect> for ConnectorService {
259276
match req.uri.scheme_str() {
260277
Some("https" | "wss") => {
261278
if let Some(ref conn) = self.ssl_pool {
262-
ctx.call(conn, req).await
279+
ctx.call(conn, req).await.map_err(ClientError::from)
263280
} else {
264-
Err(ConnectError::SslIsNotSupported)
281+
Err(ClientError::from(
282+
Error::from(ConnectError::SslIsNotSupported)
283+
.set_service(self.cfg.service()),
284+
))
265285
}
266286
}
267-
_ => ctx.call(&self.tcp_pool, req).await,
287+
_ => ctx
288+
.call(&self.tcp_pool, req)
289+
.await
290+
.map_err(ClientError::from),
268291
}
292+
.map_err(Error::from)
269293
}
270294
}
271295

0 commit comments

Comments
 (0)