Use thiserror to streamline the implementation of the main Error type

pull/168/head
WiredSound 4 years ago
parent a1b4b2de61
commit e6d66698a3
  1. 1
      Cargo.toml
  2. 62
      src/error.rs

@ -29,6 +29,7 @@ rand = "0.8.0"
sha-1 = "0.9" sha-1 = "0.9"
url = "2.1.0" url = "2.1.0"
utf-8 = "0.7.5" utf-8 = "0.7.5"
thiserror = "1.0.23"
[dependencies.native-tls] [dependencies.native-tls]
optional = true optional = true

@ -1,9 +1,10 @@
//! Error handling. //! Error handling.
use std::{error::Error as ErrorTrait, fmt, io, result, str, string}; use std::{fmt, io, result, str, string};
use crate::protocol::{frame::coding::Data, Message}; use crate::protocol::{frame::coding::Data, Message};
use http::Response; use http::Response;
use thiserror::Error;
#[cfg(feature = "tls")] #[cfg(feature = "tls")]
pub mod tls { pub mod tls {
@ -15,7 +16,7 @@ pub mod tls {
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
/// Possible WebSocket errors. /// Possible WebSocket errors.
#[derive(Debug)] #[derive(Error, Debug)]
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.
@ -28,6 +29,7 @@ pub enum Error {
/// ///
/// Receiving this error means that the WebSocket object is not usable anymore and the /// Receiving this error means that the WebSocket object is not usable anymore and the
/// only meaningful action with it is dropping it. /// only meaningful action with it is dropping it.
#[error("Connection closed normally")]
ConnectionClosed, ConnectionClosed,
/// Trying to work with already closed connection. /// Trying to work with already closed connection.
/// ///
@ -36,56 +38,39 @@ pub enum Error {
/// As opposed to `ConnectionClosed`, this indicates your code tries to operate on the /// As opposed to `ConnectionClosed`, this indicates your code tries to operate on the
/// connection when it really shouldn't anymore, so this really indicates a programmer /// connection when it really shouldn't anymore, so this really indicates a programmer
/// error on your part. /// error on your part.
#[error("Trying to work with closed connection")]
AlreadyClosed, AlreadyClosed,
/// Input-output error. Apart from WouldBlock, these are generally errors with the /// Input-output error. Apart from WouldBlock, these are generally errors with the
/// underlying connection and you should probably consider them fatal. /// underlying connection and you should probably consider them fatal.
Io(io::Error), #[error("IO error: {0}")]
Io(#[from] io::Error),
#[cfg(feature = "tls")] #[cfg(feature = "tls")]
/// TLS error. /// TLS error.
Tls(tls::Error), #[error("TLS error: {0}")]
Tls(#[from] tls::Error),
/// - 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).
#[error("Space limit exceeded: {0}")]
Capacity(CapacityErrorType), Capacity(CapacityErrorType),
/// Protocol violation. /// Protocol violation.
#[error("WebSocket protocol error: {0}")]
Protocol(ProtocolErrorType), Protocol(ProtocolErrorType),
/// Message send queue full. /// Message send queue full.
#[error("Send queue is full")]
SendQueueFull(Message), SendQueueFull(Message),
/// UTF coding error /// UTF coding error
#[error("UTF-8 encoding error")]
Utf8, Utf8,
/// Invalid URL. /// Invalid URL.
#[error("URL error: {0}")]
Url(UrlErrorType), Url(UrlErrorType),
/// HTTP error. /// HTTP error.
#[error("HTTP error: {}", .0.status())]
Http(Response<Option<String>>), Http(Response<Option<String>>),
/// HTTP format error. /// HTTP format error.
HttpFormat(http::Error), #[error("HTTP format error: {0}")]
} HttpFormat(#[from] http::Error),
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::ConnectionClosed => write!(f, "Connection closed normally"),
Error::AlreadyClosed => write!(f, "Trying to work with closed connection"),
Error::Io(ref err) => write!(f, "IO error: {}", err),
#[cfg(feature = "tls")]
Error::Tls(ref err) => write!(f, "TLS error: {}", err),
Error::Capacity(ref msg) => write!(f, "Space limit exceeded: {}", msg),
Error::Protocol(ref msg) => write!(f, "WebSocket protocol error: {}", msg),
Error::SendQueueFull(_) => write!(f, "Send queue is full"),
Error::Utf8 => write!(f, "UTF-8 encoding error"),
Error::Url(ref msg) => write!(f, "URL error: {}", msg),
Error::Http(ref code) => write!(f, "HTTP error: {}", code.status()),
Error::HttpFormat(ref err) => write!(f, "HTTP format error: {}", err),
}
}
}
impl ErrorTrait for Error {}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
} }
impl From<str::Utf8Error> for Error { impl From<str::Utf8Error> for Error {
@ -130,19 +115,6 @@ impl From<http::status::InvalidStatusCode> for Error {
} }
} }
impl From<http::Error> for Error {
fn from(err: http::Error) -> Self {
Error::HttpFormat(err)
}
}
#[cfg(feature = "tls")]
impl From<tls::Error> for Error {
fn from(err: tls::Error) -> Self {
Error::Tls(err)
}
}
impl From<httparse::Error> for Error { impl From<httparse::Error> for Error {
fn from(err: httparse::Error) -> Self { fn from(err: httparse::Error) -> Self {
match err { match err {

Loading…
Cancel
Save