From e6d66698a325cb97ea2a1c01c7ccb8baee549bfd Mon Sep 17 00:00:00 2001 From: WiredSound Date: Sat, 9 Jan 2021 21:04:41 +0000 Subject: [PATCH] Use thiserror to streamline the implementation of the main Error type --- Cargo.toml | 1 + src/error.rs | 62 ++++++++++++++-------------------------------------- 2 files changed, 18 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3e6d205..cbab804 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ rand = "0.8.0" sha-1 = "0.9" url = "2.1.0" utf-8 = "0.7.5" +thiserror = "1.0.23" [dependencies.native-tls] optional = true diff --git a/src/error.rs b/src/error.rs index 0f00064..220aed4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,9 +1,10 @@ //! 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 http::Response; +use thiserror::Error; #[cfg(feature = "tls")] pub mod tls { @@ -15,7 +16,7 @@ pub mod tls { pub type Result = result::Result; /// Possible WebSocket errors. -#[derive(Debug)] +#[derive(Error, Debug)] pub enum Error { /// WebSocket connection closed normally. This informs you of the close. /// 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 /// only meaningful action with it is dropping it. + #[error("Connection closed normally")] ConnectionClosed, /// 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 /// connection when it really shouldn't anymore, so this really indicates a programmer /// error on your part. + #[error("Trying to work with closed connection")] AlreadyClosed, /// Input-output error. Apart from WouldBlock, these are generally errors with the /// underlying connection and you should probably consider them fatal. - Io(io::Error), + #[error("IO error: {0}")] + Io(#[from] io::Error), #[cfg(feature = "tls")] /// TLS error. - Tls(tls::Error), + #[error("TLS error: {0}")] + Tls(#[from] tls::Error), /// - When reading: buffer capacity exhausted. /// - When writing: your message is bigger than the configured max message size /// (64MB by default). + #[error("Space limit exceeded: {0}")] Capacity(CapacityErrorType), /// Protocol violation. + #[error("WebSocket protocol error: {0}")] Protocol(ProtocolErrorType), /// Message send queue full. + #[error("Send queue is full")] SendQueueFull(Message), /// UTF coding error + #[error("UTF-8 encoding error")] Utf8, /// Invalid URL. + #[error("URL error: {0}")] Url(UrlErrorType), /// HTTP error. + #[error("HTTP error: {}", .0.status())] Http(Response>), /// HTTP format error. - HttpFormat(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 for Error { - fn from(err: io::Error) -> Self { - Error::Io(err) - } + #[error("HTTP format error: {0}")] + HttpFormat(#[from] http::Error), } impl From for Error { @@ -130,19 +115,6 @@ impl From for Error { } } -impl From for Error { - fn from(err: http::Error) -> Self { - Error::HttpFormat(err) - } -} - -#[cfg(feature = "tls")] -impl From for Error { - fn from(err: tls::Error) -> Self { - Error::Tls(err) - } -} - impl From for Error { fn from(err: httparse::Error) -> Self { match err {