Rename UrlErrorType to just UrlError, implement using thiserror

pull/168/head
WiredSound 4 years ago
parent 98377cf3dd
commit 3e485ddb96
  1. 14
      src/client.rs
  2. 31
      src/error.rs
  3. 8
      src/handshake/client.rs

@ -52,7 +52,7 @@ mod encryption {
use std::net::TcpStream; use std::net::TcpStream;
use crate::{ use crate::{
error::{Error, Result, UrlErrorType}, error::{Error, Result, UrlError},
stream::Mode, stream::Mode,
}; };
@ -62,7 +62,7 @@ mod encryption {
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(stream), Mode::Plain => Ok(stream),
Mode::Tls => Err(Error::Url(UrlErrorType::TlsFeatureNotEnabled)), Mode::Tls => Err(Error::Url(UrlError::TlsFeatureNotEnabled)),
} }
} }
} }
@ -71,7 +71,7 @@ use self::encryption::wrap_stream;
pub use self::encryption::AutoStream; pub use self::encryption::AutoStream;
use crate::{ use crate::{
error::{Error, Result, UrlErrorType}, error::{Error, Result, UrlError},
handshake::{client::ClientHandshake, HandshakeError}, handshake::{client::ClientHandshake, HandshakeError},
protocol::WebSocket, protocol::WebSocket,
stream::{Mode, NoDelay}, stream::{Mode, NoDelay},
@ -103,7 +103,7 @@ pub fn connect_with_config<Req: IntoClientRequest>(
) -> Result<(WebSocket<AutoStream>, Response)> { ) -> Result<(WebSocket<AutoStream>, Response)> {
let uri = request.uri(); let uri = request.uri();
let mode = uri_mode(uri)?; let mode = uri_mode(uri)?;
let host = request.uri().host().ok_or(Error::Url(UrlErrorType::NoHostName))?; let host = request.uri().host().ok_or(Error::Url(UrlError::NoHostName))?;
let port = uri.port_u16().unwrap_or(match mode { let port = uri.port_u16().unwrap_or(match mode {
Mode::Plain => 80, Mode::Plain => 80,
Mode::Tls => 443, Mode::Tls => 443,
@ -165,7 +165,7 @@ pub fn connect<Req: IntoClientRequest>(request: Req) -> Result<(WebSocket<AutoSt
} }
fn connect_to_some(addrs: &[SocketAddr], uri: &Uri, mode: Mode) -> Result<AutoStream> { fn connect_to_some(addrs: &[SocketAddr], uri: &Uri, mode: Mode) -> Result<AutoStream> {
let domain = uri.host().ok_or(Error::Url(UrlErrorType::NoHostName))?; let domain = uri.host().ok_or(Error::Url(UrlError::NoHostName))?;
for addr in addrs { for addr in addrs {
debug!("Trying to contact {} at {}...", uri, addr); debug!("Trying to contact {} at {}...", uri, addr);
if let Ok(raw_stream) = TcpStream::connect(addr) { if let Ok(raw_stream) = TcpStream::connect(addr) {
@ -174,7 +174,7 @@ fn connect_to_some(addrs: &[SocketAddr], uri: &Uri, mode: Mode) -> Result<AutoSt
} }
} }
} }
Err(Error::Url(UrlErrorType::UnableToConnect(uri.to_string()))) Err(Error::Url(UrlError::UnableToConnect(uri.to_string())))
} }
/// Get the mode of the given URL. /// Get the mode of the given URL.
@ -185,7 +185,7 @@ pub fn uri_mode(uri: &Uri) -> Result<Mode> {
match uri.scheme_str() { match uri.scheme_str() {
Some("ws") => Ok(Mode::Plain), Some("ws") => Ok(Mode::Plain),
Some("wss") => Ok(Mode::Tls), Some("wss") => Ok(Mode::Tls),
_ => Err(Error::Url(UrlErrorType::UnsupportedUrlScheme)), _ => Err(Error::Url(UrlError::UnsupportedUrlScheme)),
} }
} }

@ -1,6 +1,6 @@
//! Error handling. //! Error handling.
use std::{fmt, io, result, str, string}; use std::{io, result, str, string};
use crate::protocol::{frame::coding::Data, Message}; use crate::protocol::{frame::coding::Data, Message};
use http::Response; use http::Response;
@ -44,8 +44,8 @@ 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),
#[cfg(feature = "tls")]
/// TLS error. /// TLS error.
#[cfg(feature = "tls")]
#[error("TLS error: {0}")] #[error("TLS error: {0}")]
Tls(#[from] tls::Error), Tls(#[from] tls::Error),
/// - When reading: buffer capacity exhausted. /// - When reading: buffer capacity exhausted.
@ -59,12 +59,12 @@ pub enum Error {
/// Message send queue full. /// Message send queue full.
#[error("Send queue is full")] #[error("Send queue is full")]
SendQueueFull(Message), SendQueueFull(Message),
/// UTF coding error /// UTF coding error.
#[error("UTF-8 encoding error")] #[error("UTF-8 encoding error")]
Utf8, Utf8,
/// Invalid URL. /// Invalid URL.
#[error("URL error: {0}")] #[error("URL error: {0}")]
Url(UrlErrorType), Url(UrlError),
/// HTTP error. /// HTTP error.
#[error("HTTP error: {}", .0.status())] #[error("HTTP error: {}", .0.status())]
Http(Response<Option<String>>), Http(Response<Option<String>>),
@ -227,31 +227,24 @@ pub enum ProtocolError {
} }
/// Indicates the specific type/cause of URL error. /// Indicates the specific type/cause of URL error.
#[derive(Debug, PartialEq, Eq)] #[derive(Error, Debug, PartialEq, Eq)]
pub enum UrlErrorType { pub enum UrlError {
/// TLS is used despite not being compiled with the TLS feature enabled. /// TLS is used despite not being compiled with the TLS feature enabled.
#[error("TLS support not compiled in")]
TlsFeatureNotEnabled, TlsFeatureNotEnabled,
/// The URL does not include a host name. /// The URL does not include a host name.
#[error("No host name in the URL")]
NoHostName, NoHostName,
/// Failed to connect with this URL. /// Failed to connect with this URL.
#[error("Unable to connect to {0}")]
UnableToConnect(String), UnableToConnect(String),
/// Unsupported URL scheme used (only `ws://` or `wss://` may be used). /// Unsupported URL scheme used (only `ws://` or `wss://` may be used).
#[error("URL scheme not supported")]
UnsupportedUrlScheme, UnsupportedUrlScheme,
/// The URL host name, though included, is empty. /// The URL host name, though included, is empty.
#[error("URL contains empty host name")]
EmptyHostName, EmptyHostName,
/// The URL does not include a path/query. /// The URL does not include a path/query.
#[error("No path/query in URL")]
NoPathOrQuery, NoPathOrQuery,
} }
impl fmt::Display for UrlErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
UrlErrorType::TlsFeatureNotEnabled => write!(f, "TLS support not compiled in"),
UrlErrorType::NoHostName => write!(f, "No host name in the URL"),
UrlErrorType::UnableToConnect(uri) => write!(f, "Unable to connect to {}", uri),
UrlErrorType::UnsupportedUrlScheme => write!(f, "URL scheme not supported"),
UrlErrorType::EmptyHostName => write!(f, "URL contains empty host name"),
UrlErrorType::NoPathOrQuery => write!(f, "No path/query in URL"),
}
}
}

@ -16,7 +16,7 @@ use super::{
HandshakeRole, MidHandshake, ProcessingResult, HandshakeRole, MidHandshake, ProcessingResult,
}; };
use crate::{ use crate::{
error::{Error, ProtocolError, Result, UrlErrorType}, error::{Error, ProtocolError, Result, UrlError},
protocol::{Role, WebSocket, WebSocketConfig}, protocol::{Role, WebSocket, WebSocketConfig},
}; };
@ -97,7 +97,7 @@ fn generate_request(request: Request, key: &str) -> Result<Vec<u8>> {
let mut req = Vec::new(); let mut req = Vec::new();
let uri = request.uri(); let uri = request.uri();
let authority = uri.authority().ok_or(Error::Url(UrlErrorType::NoHostName))?.as_str(); let authority = uri.authority().ok_or(Error::Url(UrlError::NoHostName))?.as_str();
let host = if let Some(idx) = authority.find('@') { let host = if let Some(idx) = authority.find('@') {
// handle possible name:password@ // handle possible name:password@
authority.split_at(idx + 1).1 authority.split_at(idx + 1).1
@ -105,7 +105,7 @@ fn generate_request(request: Request, key: &str) -> Result<Vec<u8>> {
authority authority
}; };
if authority.is_empty() { if authority.is_empty() {
return Err(Error::Url(UrlErrorType::EmptyHostName)); return Err(Error::Url(UrlError::EmptyHostName));
} }
write!( write!(
@ -119,7 +119,7 @@ fn generate_request(request: Request, key: &str) -> Result<Vec<u8>> {
Sec-WebSocket-Key: {key}\r\n", Sec-WebSocket-Key: {key}\r\n",
version = request.version(), version = request.version(),
host = host, host = host,
path = uri.path_and_query().ok_or(Error::Url(UrlErrorType::NoPathOrQuery))?.as_str(), path = uri.path_and_query().ok_or(Error::Url(UrlError::NoPathOrQuery))?.as_str(),
key = key key = key
) )
.unwrap(); .unwrap();

Loading…
Cancel
Save