From f3857eed001462829c8fa2c7e09422df8e4756c5 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 19 Dec 2021 12:46:27 +0100 Subject: [PATCH] Ensure we provide a Transport::message() when we can --- src/error.rs | 6 +++--- src/header.rs | 2 +- src/ntls.rs | 9 +++++++-- src/response.rs | 4 +++- src/rtls.rs | 16 +++++++++++----- src/stream.rs | 13 +++++++------ src/unit.rs | 11 +++++++---- 7 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/error.rs b/src/error.rs index 3945bab..33da1f8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -356,8 +356,8 @@ impl ErrorKind { Error::new(self, None) } - pub(crate) fn msg(self, s: &str) -> Error { - Error::new(self, Some(s.to_string())) + pub(crate) fn msg(self, s: impl Into) -> Error { + Error::new(self, Some(s.into())) } } @@ -382,7 +382,7 @@ impl From for Error { impl From for Error { fn from(err: ParseError) -> Self { ErrorKind::InvalidUrl - .msg(&format!("failed to parse URL: {:?}", err)) + .msg(format!("failed to parse URL: {:?}", err)) .src(err) } } diff --git a/src/header.rs b/src/header.rs index 4297432..db9d62e 100644 --- a/src/header.rs +++ b/src/header.rs @@ -143,7 +143,7 @@ impl Header { let value_raw = &bytes[self.index + 1..]; if !valid_name(name_raw) || !valid_value(value_raw) { - Err(ErrorKind::BadHeader.msg(&format!("invalid header '{}'", self.line))) + Err(ErrorKind::BadHeader.msg(format!("invalid header '{}'", self.line))) } else { Ok(()) } diff --git a/src/ntls.rs b/src/ntls.rs index ddb33f4..1dceffe 100644 --- a/src/ntls.rs +++ b/src/ntls.rs @@ -16,8 +16,13 @@ impl TlsConnector for native_tls::TlsConnector { dns_name: &str, tcp_stream: TcpStream, ) -> Result, Error> { - let stream = native_tls::TlsConnector::connect(self, dns_name, tcp_stream) - .map_err(|e| ErrorKind::Dns.new().src(e))?; + let stream = + native_tls::TlsConnector::connect(self, dns_name, tcp_stream).map_err(|e| { + ErrorKind::ConnectionFailed + .msg("native_tls connect failed") + .src(e) + })?; + Ok(Box::new(stream)) } } diff --git a/src/response.rs b/src/response.rs index 8ee4180..2acbd70 100644 --- a/src/response.rs +++ b/src/response.rs @@ -613,7 +613,9 @@ fn parse_status_line(line: &str) -> Result<(ResponseStatusIndex, u16), Error> { return Err(BadStatus.msg("Status code was wrong length")); } - let status: u16 = status_str.parse().map_err(|_| BadStatus.new())?; + let status: u16 = status_str + .parse() + .map_err(|_| BadStatus.msg(format!("unable to parse status as u16 ({})", status_str)))?; Ok(( ResponseStatusIndex { diff --git a/src/rtls.rs b/src/rtls.rs index 43aad28..43c3fc0 100644 --- a/src/rtls.rs +++ b/src/rtls.rs @@ -95,14 +95,20 @@ impl TlsConnector for Arc { dns_name: &str, mut tcp_stream: TcpStream, ) -> Result, Error> { - let sni = - rustls::ServerName::try_from(dns_name).map_err(|e| ErrorKind::Dns.new().src(e))?; + let sni = rustls::ServerName::try_from(dns_name).map_err(|e| { + ErrorKind::Dns + .msg(format!("dns server name failed ({})", dns_name)) + .src(e) + })?; let mut sess = rustls::ClientConnection::new(self.clone(), sni) - .map_err(|e| ErrorKind::Io.new().src(e))?; + .map_err(|e| ErrorKind::Io.msg("tls connection creation failed").src(e))?; - sess.complete_io(&mut tcp_stream) - .map_err(|err| ErrorKind::ConnectionFailed.new().src(err))?; + sess.complete_io(&mut tcp_stream).map_err(|e| { + ErrorKind::ConnectionFailed + .msg("tls connection init failed") + .src(e) + })?; let stream = rustls::StreamOwned::new(sess, tcp_stream); Ok(Box::new(RustlsStream(stream))) diff --git a/src/stream.rs b/src/stream.rs index 176da0e..ede04e8 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -350,13 +350,14 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result Result { #[cfg(not(test))] pub(crate) fn connect_test(unit: &Unit) -> Result { - Err(ErrorKind::UnknownScheme.msg(&format!("unknown scheme '{}'", unit.url.scheme()))) + Err(ErrorKind::UnknownScheme.msg(format!("unknown scheme '{}'", unit.url.scheme()))) } diff --git a/src/unit.rs b/src/unit.rs index bb2c9d5..fbeb172 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -168,7 +168,10 @@ pub(crate) fn connect( break resp; } if history.len() + 1 >= unit.agent.config.redirects as usize { - return Err(ErrorKind::TooManyRedirects.new()); + return Err(ErrorKind::TooManyRedirects.msg(format!( + "reached max redirects ({})", + unit.agent.config.redirects + ))); } // the location header let location = match resp.header("location") { @@ -181,7 +184,7 @@ pub(crate) fn connect( // join location header to current url in case it is relative let new_url = url.join(location).map_err(|e| { ErrorKind::InvalidUrl - .msg(&format!("Bad redirection: {}", location)) + .msg(format!("Bad redirection: {}", location)) .src(e) })?; @@ -340,7 +343,7 @@ fn extract_cookies(agent: &Agent, url: &Url) -> Option
{ fn connect_socket(unit: &Unit, hostname: &str, use_pooled: bool) -> Result<(Stream, bool), Error> { match unit.url.scheme() { "http" | "https" | "test" => (), - scheme => return Err(ErrorKind::UnknownScheme.msg(&format!("unknown scheme '{}'", scheme))), + scheme => return Err(ErrorKind::UnknownScheme.msg(format!("unknown scheme '{}'", scheme))), }; if use_pooled { let pool = &unit.agent.state.pool; @@ -360,7 +363,7 @@ fn connect_socket(unit: &Unit, hostname: &str, use_pooled: bool) -> Result<(Stre "http" => stream::connect_http(unit, hostname), "https" => stream::connect_https(unit, hostname), "test" => connect_test(unit), - scheme => Err(ErrorKind::UnknownScheme.msg(&format!("unknown scheme {}", scheme))), + scheme => Err(ErrorKind::UnknownScheme.msg(format!("unknown scheme {}", scheme))), }; Ok((stream?, false)) }