Skip to content

Commit bbe0575

Browse files
committed
Service name is static
1 parent d007119 commit bbe0575

File tree

29 files changed

+355
-520
lines changed

29 files changed

+355
-520
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ntex-codec = "1.1.0"
6161
ntex-error = "1.0.0"
6262
ntex-io = "3.9.0"
6363
ntex-dispatcher = "3.1.0"
64-
ntex-net = "3.7.0"
64+
ntex-net = "3.8.0"
6565
ntex-http = "1.0.0"
6666
ntex-router = "1.0.0"
6767
ntex-rt = "3.9.0"

ntex-error/src/lib.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub trait ErrorDiagnostic: error::Error + 'static {
4141
fn kind(&self) -> Self::Kind;
4242

4343
/// Provides a string to identify responsible service
44-
fn service(&self) -> Option<ByteString> {
44+
fn service(&self) -> Option<&'static str> {
4545
None
4646
}
4747

@@ -77,7 +77,7 @@ pub trait ErrorInfo {
7777

7878
fn error_signature(&self) -> ByteString;
7979

80-
fn service(&self) -> Option<ByteString>;
80+
fn service(&self) -> Option<&'static str>;
8181

8282
fn signature(&self) -> &'static str;
8383

@@ -95,14 +95,14 @@ trait Traversable<K>: ErrorDiagnostic<Kind = K> {
9595
pub struct Error<E> {
9696
#[source]
9797
error: E,
98-
service: Option<ByteString>,
98+
service: Option<&'static str>,
9999
location: &'static Location<'static>,
100100
}
101101

102102
impl<E> Error<E> {
103103
pub const fn new(
104104
error: E,
105-
service: Option<ByteString>,
105+
service: Option<&'static str>,
106106
location: &'static Location<'static>,
107107
) -> Self {
108108
Self {
@@ -113,7 +113,7 @@ impl<E> Error<E> {
113113
}
114114

115115
/// Set response service
116-
pub fn set_service(mut self, name: ByteString) -> Self {
116+
pub fn set_service(mut self, name: &'static str) -> Self {
117117
self.service = Some(name);
118118
self
119119
}
@@ -145,6 +145,8 @@ impl<E> From<E> for Error<E> {
145145
}
146146
}
147147

148+
impl<E> Eq for Error<E> where E: Eq {}
149+
148150
impl<E> PartialEq for Error<E>
149151
where
150152
E: PartialEq,
@@ -154,7 +156,14 @@ where
154156
}
155157
}
156158

157-
impl<E> Eq for Error<E> where E: Eq {}
159+
impl<E> PartialEq<E> for Error<E>
160+
where
161+
E: PartialEq,
162+
{
163+
fn eq(&self, other: &E) -> bool {
164+
self.error.eq(other)
165+
}
166+
}
158167

159168
impl<E> ops::Deref for Error<E> {
160169
type Target = E;
@@ -174,8 +183,12 @@ where
174183
self.error.kind()
175184
}
176185

177-
fn service(&self) -> Option<ByteString> {
178-
self.service.clone()
186+
fn service(&self) -> Option<&'static str> {
187+
if self.service.is_some() {
188+
self.service
189+
} else {
190+
self.error.service()
191+
}
179192
}
180193

181194
fn signature(&self) -> &'static str {
@@ -229,8 +242,8 @@ where
229242
fn from(err: Error<E>) -> Self {
230243
Self {
231244
error: Arc::new(ErrorChainWrapper {
245+
service: err.service(),
232246
error: err.error,
233-
service: err.service,
234247
location: err.location,
235248
_k: PhantomData,
236249
}),
@@ -248,7 +261,7 @@ where
248261
self.error.kind()
249262
}
250263

251-
fn service(&self) -> Option<ByteString> {
264+
fn service(&self) -> Option<&'static str> {
252265
self.error.service()
253266
}
254267

@@ -270,7 +283,7 @@ where
270283
struct ErrorChainWrapper<E: Sized, K> {
271284
#[source]
272285
error: E,
273-
service: Option<ByteString>,
286+
service: Option<&'static str>,
274287
location: &'static Location<'static>,
275288
_k: PhantomData<K>,
276289
}
@@ -298,8 +311,8 @@ where
298311
self.error.kind().into()
299312
}
300313

301-
fn service(&self) -> Option<ByteString> {
302-
self.service.clone()
314+
fn service(&self) -> Option<&'static str> {
315+
self.service
303316
}
304317

305318
fn signature(&self) -> &'static str {
@@ -340,7 +353,7 @@ impl<'a, E: ErrorDiagnostic> ErrorInfo for ErrorInfoWrapper<'a, E> {
340353
ByteString::try_from(buf).unwrap()
341354
}
342355

343-
fn service(&self) -> Option<ByteString> {
356+
fn service(&self) -> Option<&'static str> {
344357
self.inner.service()
345358
}
346359

@@ -405,8 +418,8 @@ mod tests {
405418
}
406419
}
407420

408-
fn service(&self) -> Option<ByteString> {
409-
Some(ByteString::from_static("test"))
421+
fn service(&self) -> Option<&'static str> {
422+
Some("test")
410423
}
411424

412425
fn signature(&self) -> &'static str {
@@ -424,7 +437,7 @@ mod tests {
424437
assert_eq!(err.kind(), TestKind::ServiceError);
425438
assert_eq!((*err).kind(), TestKind::ServiceError);
426439
assert_eq!(err.to_string(), "InternalServiceError");
427-
assert_eq!(err.service(), Some(ByteString::from_static("test")));
440+
assert_eq!(err.service(), Some("test"));
428441
assert_eq!(
429442
err,
430443
Into::<Error<TestError>>::into(TestError::Service("409 Error"))
@@ -437,9 +450,12 @@ mod tests {
437450
assert_eq!(info.error_signature(), "ServiceError");
438451
assert_eq!(info.signature(), "Service-Internal");
439452
assert_eq!(info.description(), "InternalServiceError");
440-
assert_eq!(info.service(), Some(ByteString::from_static("test")));
453+
assert_eq!(info.service(), Some("test"));
441454
});
442455

456+
let err = err.set_service("SVC");
457+
assert_eq!(err.service(), Some("SVC"));
458+
443459
assert_eq!(
444460
TestError::Connect("").kind().error_type(),
445461
ErrorType::ClientError
@@ -454,10 +470,7 @@ mod tests {
454470
);
455471
assert_eq!(TestError::Connect("").to_string(), "Connect err: ");
456472
assert_eq!(TestError::Disconnect.to_string(), "Disconnect");
457-
assert_eq!(
458-
TestError::Disconnect.service(),
459-
Some(ByteString::from_static("test"))
460-
);
473+
assert_eq!(TestError::Disconnect.service(), Some("test"));
461474
assert!(TestError::Disconnect.location().is_none());
462475

463476
TestError::Connect("").traverse(&mut |info| {
@@ -498,7 +511,7 @@ mod tests {
498511
let err: ErrorChain<TestKind> = err.into();
499512
assert_eq!(err.kind(), TestKind::ServiceError);
500513
assert_eq!(err.kind(), TestError::Service("404 Error").kind());
501-
assert_eq!(err.service(), Some(ByteString::from_static("test")));
514+
assert_eq!(err.service(), Some("test"));
502515
assert_eq!(err.signature(), "Service-Internal");
503516
assert_eq!(err.to_string(), "InternalServiceError");
504517
assert!(err.location().is_some());

ntex-net/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.8.0] - 2026-03-xx
4+
5+
* Use ntex_error::Error for connect service
6+
37
## [3.7.0] - 2026-02-16
48

59
* SharedCfg is not Copy

ntex-net/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-net"
3-
version = "3.7.0"
3+
version = "3.8.0"
44
authors = ["ntex contributors <team@ntex.rs>"]
55
description = "ntexwork utils for ntex framework"
66
keywords = ["network", "framework", "async", "futures"]

ntex-net/src/connect/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl<T: Address> ConnectorService<T> {
8686
)))
8787
})
8888
.and_then(|item| item)
89+
.map_err(|e| e.set_service(self.shared.service()))
8990
}
9091
}
9192

ntex-tls/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.4.0] - 2026-03-xx
4+
5+
* Use ntex_error::Error for connect services
6+
37
## [3.3.0] - 2026-02-16
48

59
* SharedCfg is not Copy

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

0 commit comments

Comments
 (0)