Move TLS related errors to a separate enum

pull/166/head
Dominik Nakamura 4 years ago
parent 330d08dfdb
commit d70aa8feec
No known key found for this signature in database
GPG Key ID: E4C6A749B2491910
  1. 16
      src/client.rs
  2. 41
      src/error.rs

@ -26,17 +26,20 @@ mod encryption {
/// TCP stream switcher (plain/TLS). /// TCP stream switcher (plain/TLS).
pub type AutoStream = StreamSwitcher<TcpStream, TlsStream<TcpStream>>; pub type AutoStream = StreamSwitcher<TcpStream, TlsStream<TcpStream>>;
use crate::{error::Result, stream::Mode}; use crate::{
error::{Result, TlsError},
stream::Mode,
};
pub fn wrap_stream(stream: TcpStream, domain: &str, mode: Mode) -> Result<AutoStream> { pub fn wrap_stream(stream: TcpStream, domain: &str, mode: Mode) -> Result<AutoStream> {
match mode { match mode {
Mode::Plain => Ok(StreamSwitcher::Plain(stream)), Mode::Plain => Ok(StreamSwitcher::Plain(stream)),
Mode::Tls => { Mode::Tls => {
let connector = TlsConnector::builder().build()?; let connector = TlsConnector::builder().build().map_err(TlsError::Native)?;
connector connector
.connect(domain, stream) .connect(domain, stream)
.map_err(|e| match e { .map_err(|e| match e {
TlsHandshakeError::Failure(f) => f.into(), TlsHandshakeError::Failure(f) => TlsError::Native(f).into(),
TlsHandshakeError::WouldBlock(_) => { TlsHandshakeError::WouldBlock(_) => {
panic!("Bug: TLS handshake not blocked") panic!("Bug: TLS handshake not blocked")
} }
@ -58,7 +61,10 @@ mod encryption {
/// TCP stream switcher (plain/TLS). /// TCP stream switcher (plain/TLS).
pub type AutoStream = StreamSwitcher<TcpStream, StreamOwned<ClientSession, TcpStream>>; pub type AutoStream = StreamSwitcher<TcpStream, StreamOwned<ClientSession, TcpStream>>;
use crate::{error::Result, stream::Mode}; use crate::{
error::{Result, TlsError},
stream::Mode,
};
pub fn wrap_stream(stream: TcpStream, domain: &str, mode: Mode) -> Result<AutoStream> { pub fn wrap_stream(stream: TcpStream, domain: &str, mode: Mode) -> Result<AutoStream> {
match mode { match mode {
@ -70,7 +76,7 @@ mod encryption {
Arc::new(config) Arc::new(config)
}; };
let domain = DNSNameRef::try_from_ascii_str(domain)?; let domain = DNSNameRef::try_from_ascii_str(domain).map_err(TlsError::Dns)?;
let client = ClientSession::new(&config, domain); let client = ClientSession::new(&config, domain);
let stream = StreamOwned::new(client, stream); let stream = StreamOwned::new(client, stream);

@ -11,7 +11,6 @@ pub type Result<T> = result::Result<T, Error>;
/// Possible WebSocket errors. /// Possible WebSocket errors.
#[derive(Error, Debug)] #[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error { pub enum Error {
/// WebSocket connection closed normally. This informs you of the close. /// WebSocket connection closed normally. This informs you of the close.
/// It's not an error as such and nothing wrong happened. /// It's not an error as such and nothing wrong happened.
@ -39,18 +38,12 @@ pub enum Error {
/// underlying connection and you should probably consider them fatal. /// underlying connection and you should probably consider them fatal.
#[error("IO error: {0}")] #[error("IO error: {0}")]
Io(#[from] io::Error), Io(#[from] io::Error),
/// TLS error /// TLS error.
#[cfg(feature = "native-tls")] ///
#[error("TLS (native-tls) error: {0}")] /// Note that this error variant is enabled unconditionally even if no TLS feature is enabled,
TlsNative(#[from] native_tls_crate::Error), /// to provide a feature-agnostic API surface.
/// TLS error #[error("TLS error: {0}")]
#[cfg(feature = "rustls-tls")] TlsNative(#[from] TlsError),
#[error("TLS (rustls) error: {0}")]
TlsRustls(#[from] rustls::TLSError),
/// DNS name resolution error.
#[cfg(feature = "rustls-tls")]
#[error("Invalid DNS name: {0}")]
Dns(#[from] webpki::InvalidDNSNameError),
/// - When reading: buffer capacity exhausted. /// - When reading: buffer capacity exhausted.
/// - When writing: your message is bigger than the configured max message size /// - When writing: your message is bigger than the configured max message size
/// (64MB by default). /// (64MB by default).
@ -251,3 +244,25 @@ pub enum UrlError {
#[error("No path/query in URL")] #[error("No path/query in URL")]
NoPathOrQuery, NoPathOrQuery,
} }
/// TLS errors.
///
/// Note that even if you enable only the rustls-based TLS support, the error at runtime could still
/// be `Native`, as another crate in the dependency graph may enable native TLS support.
#[allow(missing_copy_implementations)]
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum TlsError {
/// Native TLS error.
#[cfg(feature = "native-tls")]
#[error("native-tls error: {0}")]
Native(#[from] native_tls_crate::Error),
/// Rustls error.
#[cfg(feature = "rustls-tls")]
#[error("rustls error: {0}")]
Rustls(#[from] rustls::TLSError),
/// DNS name resolution error.
#[cfg(feature = "rustls-tls")]
#[error("Invalid DNS name: {0}")]
Dns(#[from] webpki::InvalidDNSNameError),
}

Loading…
Cancel
Save