Skip to content

Commit ab35a06

Browse files
committed
Service name is static
1 parent 030cf31 commit ab35a06

File tree

24 files changed

+309
-495
lines changed

24 files changed

+309
-495
lines changed

ntex-net/src/connect/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl<T: Address> ConnectorService<T> {
9292
)))
9393
})
9494
.and_then(|item| item)
95+
.map_err(|e| e.set_service(self.shared.service()))
9596
}
9697
}
9798

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-tls/src/openssl/connect.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,39 +76,35 @@ where
7676
let tag = io.tag();
7777
log::trace!("{tag}: SSL Handshake start for: {host:?}");
7878

79-
match self.openssl.configure() {
80-
Err(e) => Err(ConnectError::from(io::Error::new(
81-
io::ErrorKind::InvalidInput,
82-
e,
83-
))
84-
.into()),
85-
Ok(config) => {
86-
let ssl = config.into_ssl(&host).map_err(|e| {
87-
ConnectError::from(io::Error::new(io::ErrorKind::InvalidInput, e))
88-
})?;
89-
90-
match timeout_checked(self.cfg.handshake_timeout(), connect_io(io, ssl))
91-
.await
92-
{
93-
Ok(Ok(io)) => {
94-
log::trace!("{tag}: SSL Handshake success: {host:?}");
95-
Ok(io)
96-
}
97-
Ok(Err(e)) => {
98-
log::trace!("{tag}: SSL Handshake error: {e:?}");
99-
Err(ConnectError::from(e).into())
100-
}
101-
Err(()) => {
102-
log::trace!("{tag}: SSL Handshake timeout");
103-
Err(ConnectError::from(io::Error::new(
104-
io::ErrorKind::TimedOut,
105-
"SSL Handshake timeout",
106-
))
107-
.into())
108-
}
79+
async {
80+
let config = self.openssl.configure().map_err(|e| {
81+
ConnectError::from(io::Error::new(io::ErrorKind::InvalidInput, e))
82+
})?;
83+
let ssl = config.into_ssl(&host).map_err(|e| {
84+
ConnectError::from(io::Error::new(io::ErrorKind::InvalidInput, e))
85+
})?;
86+
87+
match timeout_checked(self.cfg.handshake_timeout(), connect_io(io, ssl)).await {
88+
Ok(Ok(io)) => {
89+
log::trace!("{tag}: SSL Handshake success: {host:?}");
90+
Ok(io)
91+
}
92+
Ok(Err(e)) => {
93+
log::trace!("{tag}: SSL Handshake error: {e:?}");
94+
Err(ConnectError::from(e).into())
95+
}
96+
Err(()) => {
97+
log::trace!("{tag}: SSL Handshake timeout");
98+
Err(ConnectError::from(io::Error::new(
99+
io::ErrorKind::TimedOut,
100+
"SSL Handshake timeout",
101+
))
102+
.into())
109103
}
110104
}
111105
}
106+
.await
107+
.map_err(|e: Error<_>| e.set_service(self.cfg.service()))
112108
}
113109
}
114110

ntex-tls/src/rustls/connect.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,33 @@ where
108108
log::trace!("{tag}: TLS Handshake start for: {host:?}");
109109

110110
let config = self.config.clone();
111-
let host = ServerName::try_from(host)
112-
.map_err(|e| ConnectError::from(io::Error::other(e)))?;
113-
114-
let connect_fut = TlsClientFilter::create(io, config, host.clone());
115-
match timeout_checked(self.cfg.handshake_timeout(), connect_fut).await {
116-
Ok(Ok(io)) => {
117-
log::trace!("{tag}: TLS Handshake success: {host:?}");
118-
Ok(io)
119-
}
120-
Ok(Err(e)) => {
121-
log::trace!("{tag}: TLS Handshake error: {e:?}");
122-
Err(ConnectError::from(e).into())
123-
}
124-
Err(()) => {
125-
log::trace!("{tag}: TLS Handshake timeout");
126-
Err(ConnectError::from(io::Error::new(
127-
io::ErrorKind::TimedOut,
128-
"SSL Handshake timeout",
129-
))
130-
.into())
111+
112+
async {
113+
let host = ServerName::try_from(host)
114+
.map_err(|e| ConnectError::from(io::Error::other(e)))?;
115+
116+
let connect_fut = TlsClientFilter::create(io, config, host.clone());
117+
match timeout_checked(self.cfg.handshake_timeout(), connect_fut).await {
118+
Ok(Ok(io)) => {
119+
log::trace!("{tag}: TLS Handshake success: {host:?}");
120+
Ok(io)
121+
}
122+
Ok(Err(e)) => {
123+
log::trace!("{tag}: TLS Handshake error: {e:?}");
124+
Err(ConnectError::from(e).into())
125+
}
126+
Err(()) => {
127+
log::trace!("{tag}: TLS Handshake timeout");
128+
Err(ConnectError::from(io::Error::new(
129+
io::ErrorKind::TimedOut,
130+
"SSL Handshake timeout",
131+
))
132+
.into())
133+
}
131134
}
132135
}
136+
.await
137+
.map_err(|e: Error<_>| e.set_service(self.cfg.service()))
133138
}
134139
}
135140

ntex/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [3.6.0] - 2026-03-xx
4+
5+
* Use ntex_error::Error for http client
6+
37
## [3.5.1] - 2026-02-19
48

59
* Fix http client request version

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: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::connect::{Connect as TcpConnect, Connector as TcpConnector};
44
use crate::service::{Service, ServiceCtx, ServiceFactory, apply_fn_factory, boxed};
55
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;
@@ -197,7 +198,7 @@ impl Connector {
197198

198199
impl ServiceFactory<Connect, SharedCfg> for Connector {
199200
type Response = Connection;
200-
type Error = Error<ConnectError>;
201+
type Error = Error<ClientError>;
201202
type Service = ConnectorService;
202203
type InitError = Box<dyn StdError>;
203204

@@ -218,39 +219,44 @@ impl ServiceFactory<Connect, SharedCfg> for Connector {
218219
self.conn_lifetime,
219220
self.conn_keep_alive,
220221
self.limit,
221-
cfg,
222+
cfg.clone(),
222223
);
223-
Ok(ConnectorService { tcp_pool, ssl_pool })
224+
Ok(ConnectorService {
225+
cfg,
226+
tcp_pool,
227+
ssl_pool,
228+
})
224229
}
225230
}
226231

227232
/// Manages http client network connectivity.
228233
#[derive(Clone, Debug)]
229234
pub struct ConnectorService {
235+
cfg: SharedCfg,
230236
tcp_pool: ConnectionPool,
231237
ssl_pool: Option<ConnectionPool>,
232238
}
233239

234240
impl Service<Connect> for ConnectorService {
235241
type Response = Connection;
236-
type Error = Error<ConnectError>;
242+
type Error = Error<ClientError>;
237243

238244
#[inline]
239245
async fn ready(&self, ctx: ServiceCtx<'_, Self>) -> Result<(), Self::Error> {
240246
if let Some(ref ssl_pool) = self.ssl_pool {
241247
let (r1, r2) = join(ctx.ready(&self.tcp_pool), ctx.ready(ssl_pool)).await;
242-
r1?;
243-
r2
248+
r1.map_err(ClientError::from)?;
249+
Ok(r2.map_err(ClientError::from)?)
244250
} else {
245-
ctx.ready(&self.tcp_pool).await
251+
Ok(ctx.ready(&self.tcp_pool).await.map_err(ClientError::from)?)
246252
}
247253
}
248254

249255
#[inline]
250256
fn poll(&self, cx: &mut Context<'_>) -> Result<(), Self::Error> {
251-
self.tcp_pool.poll(cx)?;
257+
self.tcp_pool.poll(cx).map_err(ClientError::from)?;
252258
if let Some(ref ssl_pool) = self.ssl_pool {
253-
ssl_pool.poll(cx)?;
259+
ssl_pool.poll(cx).map_err(ClientError::from)?;
254260
}
255261
Ok(())
256262
}
@@ -270,13 +276,20 @@ impl Service<Connect> for ConnectorService {
270276
match req.uri.scheme_str() {
271277
Some("https" | "wss") => {
272278
if let Some(ref conn) = self.ssl_pool {
273-
ctx.call(conn, req).await
279+
ctx.call(conn, req).await.map_err(ClientError::from)
274280
} else {
275-
Err(ConnectError::SslIsNotSupported.into())
281+
Err(ClientError::from(
282+
Error::from(ConnectError::SslIsNotSupported)
283+
.set_service(self.cfg.service()),
284+
))
276285
}
277286
}
278-
_ => ctx.call(&self.tcp_pool, req).await,
287+
_ => ctx
288+
.call(&self.tcp_pool, req)
289+
.await
290+
.map_err(ClientError::from),
279291
}
292+
.map_err(Error::from)
280293
}
281294
}
282295

ntex/src/client/error.rs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub enum InvalidUrl {
143143

144144
/// A set of errors that can occur during request sending and response reading
145145
#[derive(thiserror::Error, Debug)]
146-
pub enum SendRequestError {
146+
pub enum ClientError {
147147
/// Invalid URL
148148
#[error("Invalid URL: {0}")]
149149
Url(#[from] InvalidUrl),
@@ -176,39 +176,63 @@ pub enum SendRequestError {
176176
Error(#[from] Rc<dyn StdError>),
177177
}
178178

179-
impl Clone for SendRequestError {
180-
fn clone(&self) -> SendRequestError {
179+
impl ErrorDiagnostic for ClientError {
180+
type Kind = ErrorType;
181+
182+
fn kind(&self) -> ErrorType {
181183
match self {
182-
SendRequestError::Url(err) => SendRequestError::Url(err.clone()),
183-
SendRequestError::Connect(err) => SendRequestError::Connect(err.clone()),
184-
SendRequestError::Request(err) => SendRequestError::Request(err.clone()),
185-
SendRequestError::Response(err) => SendRequestError::Response(*err),
186-
SendRequestError::Http(err) => SendRequestError::Http(err.clone()),
187-
SendRequestError::H2(err) => SendRequestError::H2(err.clone()),
188-
SendRequestError::Timeout => SendRequestError::Timeout,
189-
SendRequestError::TunnelNotSupported => SendRequestError::TunnelNotSupported,
190-
SendRequestError::Error(err) => SendRequestError::Error(err.clone()),
191-
SendRequestError::Send(err) => {
192-
SendRequestError::Send(crate::util::clone_io_error(err))
193-
}
184+
ClientError::Url(_) | ClientError::Http(_) => ErrorType::ClientError,
185+
ClientError::Connect(err) => err.kind(),
186+
ClientError::Send(_)
187+
| ClientError::Request(_)
188+
| ClientError::Response(_)
189+
| ClientError::H2(_)
190+
| ClientError::Timeout
191+
| ClientError::TunnelNotSupported
192+
| ClientError::Error(_) => ErrorType::ServiceError,
193+
}
194+
}
195+
196+
fn service(&self) -> Option<&'static str> {
197+
if let ClientError::Connect(err) = self {
198+
err.service()
199+
} else {
200+
None
194201
}
195202
}
196203
}
197204

198-
impl From<Either<EncodeError, io::Error>> for SendRequestError {
205+
impl Clone for ClientError {
206+
fn clone(&self) -> ClientError {
207+
match self {
208+
ClientError::Url(err) => ClientError::Url(err.clone()),
209+
ClientError::Connect(err) => ClientError::Connect(err.clone()),
210+
ClientError::Request(err) => ClientError::Request(err.clone()),
211+
ClientError::Response(err) => ClientError::Response(*err),
212+
ClientError::Http(err) => ClientError::Http(err.clone()),
213+
ClientError::H2(err) => ClientError::H2(err.clone()),
214+
ClientError::Timeout => ClientError::Timeout,
215+
ClientError::TunnelNotSupported => ClientError::TunnelNotSupported,
216+
ClientError::Error(err) => ClientError::Error(err.clone()),
217+
ClientError::Send(err) => ClientError::Send(crate::util::clone_io_error(err)),
218+
}
219+
}
220+
}
221+
222+
impl From<Either<EncodeError, io::Error>> for ClientError {
199223
fn from(err: Either<EncodeError, io::Error>) -> Self {
200224
match err {
201-
Either::Left(err) => SendRequestError::Request(err),
202-
Either::Right(err) => SendRequestError::Send(err),
225+
Either::Left(err) => ClientError::Request(err),
226+
Either::Right(err) => ClientError::Send(err),
203227
}
204228
}
205229
}
206230

207-
impl From<Either<DecodeError, io::Error>> for SendRequestError {
231+
impl From<Either<DecodeError, io::Error>> for ClientError {
208232
fn from(err: Either<DecodeError, io::Error>) -> Self {
209233
match err {
210-
Either::Left(err) => SendRequestError::Response(err),
211-
Either::Right(err) => SendRequestError::Send(err),
234+
Either::Left(err) => ClientError::Response(err),
235+
Either::Right(err) => ClientError::Send(err),
212236
}
213237
}
214238
}
@@ -218,3 +242,7 @@ impl From<ClientBuilderError> for io::Error {
218242
io::Error::other(err)
219243
}
220244
}
245+
246+
#[doc(hidden)]
247+
#[deprecated(since = "3.6.0", note = "Use ntex::client::error::ClientError instead")]
248+
pub type SendRequestError = ClientError;

0 commit comments

Comments
 (0)