From e71a7f3cf26c26c38089b847358747baa6fe1279 Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Wed, 11 Mar 2020 23:39:41 -0300 Subject: [PATCH] awc: Convert from `derive_more` to `thiserror` The `thiserror` has the advantage of implementing `std::error::Error` and it integrates better with the Rust ecosystem. Signed-off-by: Otavio Salvador --- awc/Cargo.toml | 2 +- awc/src/error.rs | 36 ++++++++++++++++++------------------ awc/src/sender.rs | 10 ++++++---- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/awc/Cargo.toml b/awc/Cargo.toml index fde136eb0..aca64439b 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -41,7 +41,6 @@ actix-rt = "1.0.0" base64 = "0.11" bytes = "0.5.3" -derive_more = "0.99.2" futures-core = "0.3.1" log =" 0.4" mime = "0.3" @@ -52,6 +51,7 @@ serde_json = "1.0" serde_urlencoded = "0.6.1" open-ssl = { version="0.10", package="openssl", optional = true } rust-tls = { version = "0.17.0", package="rustls", optional = true, features = ["dangerous_configuration"] } +thiserror = "1.0.11" [dev-dependencies] actix-connect = { version = "2.0.0-alpha.2", features=["openssl"] } diff --git a/awc/src/error.rs b/awc/src/error.rs index 7fece74ee..c5b954858 100644 --- a/awc/src/error.rs +++ b/awc/src/error.rs @@ -11,35 +11,35 @@ use actix_http::ResponseError; use serde_json::error::Error as JsonError; use actix_http::http::{header::HeaderValue, StatusCode}; -use derive_more::{Display, From}; +use thiserror::Error; /// Websocket client error -#[derive(Debug, Display, From)] +#[derive(Debug, Error)] pub enum WsClientError { /// Invalid response status - #[display(fmt = "Invalid response status")] + #[error("Invalid response status")] InvalidResponseStatus(StatusCode), /// Invalid upgrade header - #[display(fmt = "Invalid upgrade header")] + #[error("Invalid upgrade header")] InvalidUpgradeHeader, /// Invalid connection header - #[display(fmt = "Invalid connection header")] + #[error("Invalid connection header")] InvalidConnectionHeader(HeaderValue), /// Missing CONNECTION header - #[display(fmt = "Missing CONNECTION header")] + #[error("Missing CONNECTION header")] MissingConnectionHeader, /// Missing SEC-WEBSOCKET-ACCEPT header - #[display(fmt = "Missing SEC-WEBSOCKET-ACCEPT header")] + #[error("Missing SEC-WEBSOCKET-ACCEPT header")] MissingWebSocketAcceptHeader, /// Invalid challenge response - #[display(fmt = "Invalid challenge response")] + #[error("Invalid challenge response")] InvalidChallengeResponse(String, HeaderValue), /// Protocol error - #[display(fmt = "{}", _0)] - Protocol(WsProtocolError), + #[error(transparent)] + Protocol(#[from] WsProtocolError), /// Send request error - #[display(fmt = "{}", _0)] - SendRequest(SendRequestError), + #[error(transparent)] + SendRequest(#[from] SendRequestError), } impl From for WsClientError { @@ -55,17 +55,17 @@ impl From for WsClientError { } /// A set of errors that can occur during parsing json payloads -#[derive(Debug, Display, From)] +#[derive(Debug, Error)] pub enum JsonPayloadError { /// Content type error - #[display(fmt = "Content type error")] + #[error("Content type error")] ContentType, /// Deserialize error - #[display(fmt = "Json deserialize error: {}", _0)] - Deserialize(JsonError), + #[error("Json deserialize error: {0}")] + Deserialize(#[from] JsonError), /// Payload error - #[display(fmt = "Error that occur during reading payload: {}", _0)] - Payload(PayloadError), + #[error("Error that occur during reading payload: {0}")] + Payload(#[from] PayloadError), } /// Return `InternalServerError` for `JsonPayloadError` diff --git a/awc/src/sender.rs b/awc/src/sender.rs index 983e730e1..920b069db 100644 --- a/awc/src/sender.rs +++ b/awc/src/sender.rs @@ -6,9 +6,9 @@ use std::time::Duration; use actix_rt::time::{delay_for, Delay}; use bytes::Bytes; -use derive_more::From; use futures_core::{Future, Stream}; use serde::Serialize; +use thiserror::Error; use actix_http::body::{Body, BodyStream}; use actix_http::http::header::{self, IntoHeaderValue}; @@ -26,10 +26,12 @@ use crate::error::{FreezeRequestError, InvalidUrl, SendRequestError}; use crate::response::ClientResponse; use crate::ClientConfig; -#[derive(Debug, From)] +#[derive(Debug, Error)] pub(crate) enum PrepForSendingError { - Url(InvalidUrl), - Http(HttpError), + #[error(transparent)] + Url(#[from] InvalidUrl), + #[error(transparent)] + Http(#[from] HttpError), } impl Into for PrepForSendingError {