From 5b387a858a97c0a996644c9b561561e46a834bcc Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 28 Feb 2019 16:34:30 +0000 Subject: [PATCH 1/2] ws: replace error-chain with vanilla Error impl --- ws/Cargo.toml | 1 - ws/src/error.rs | 43 ++++++++++++++++++++++++++++++++----------- ws/src/lib.rs | 4 +--- ws/src/metadata.rs | 2 +- ws/src/session.rs | 2 +- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/ws/Cargo.toml b/ws/Cargo.toml index 5b6e7b8f7..3c6887455 100644 --- a/ws/Cargo.toml +++ b/ws/Cargo.toml @@ -10,7 +10,6 @@ repository = "https://github.com/paritytech/jsonrpc" version = "10.1.0" [dependencies] -error-chain = "0.12" jsonrpc-core = { version = "10.1", path = "../core" } jsonrpc-server-utils = { version = "10.1", path = "../server-utils" } log = "0.4" diff --git a/ws/src/error.rs b/ws/src/error.rs index c1b0ceea1..34b122ebb 100644 --- a/ws/src/error.rs +++ b/ws/src/error.rs @@ -1,28 +1,49 @@ #![allow(missing_docs)] -use std::io; +use std::{io, error, fmt, result}; use crate::ws; -error_chain! { - foreign_links { - Io(io::Error); +#[derive(Debug)] +pub enum Error { + Io(io::Error), + WsError(ws::Error), + ConnectionClosed, +} + +pub type Result = result::Result; + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> { + match self { + Error::ConnectionClosed => write!(f, "Action on closed connection."), + Error::WsError(err) => write!(f, "WebSockets Error: {}", err), + Error::Io(err) => write!(f, "Io Error: {}", err), + } } +} - errors { - /// Attempted action on closed connection. - ConnectionClosed { - description("connection is closed"), - display("Action on closed connection."), +impl error::Error for Error { + fn source(&self) -> Option<&(error::Error + 'static)> { + match self { + Error::Io(io) => Some(io), + Error::WsError(ws) => Some(ws), + Error::ConnectionClosed => None, } } } +impl From for Error { + fn from(err: io::Error) -> Self { + Error::Io(err) + } +} + impl From for Error { fn from(err: ws::Error) -> Self { match err.kind { - ws::ErrorKind::Io(e) => e.into(), - _ => Error::with_chain(err, "WebSockets Error"), + ws::ErrorKind::Io(err) => Error::Io(err), + _ => Error::WsError(err), } } } diff --git a/ws/src/lib.rs b/ws/src/lib.rs index 1f34c0d48..7eb64b09d 100644 --- a/ws/src/lib.rs +++ b/ws/src/lib.rs @@ -7,8 +7,6 @@ use jsonrpc_server_utils as server_utils; pub use ws; pub use jsonrpc_core; -#[macro_use] -extern crate error_chain; #[macro_use] extern crate log; @@ -22,7 +20,7 @@ mod tests; use jsonrpc_core as core; -pub use self::error::{Error, ErrorKind, Result}; +pub use self::error::{Error, Result}; pub use self::metadata::{RequestContext, MetaExtractor, NoopExtractor}; pub use self::session::{RequestMiddleware, MiddlewareAction}; pub use self::server::{CloseHandle, Server}; diff --git a/ws/src/metadata.rs b/ws/src/metadata.rs index 8a184cd97..f9c27e503 100644 --- a/ws/src/metadata.rs +++ b/ws/src/metadata.rs @@ -29,7 +29,7 @@ impl Sender { if self.active.load(atomic::Ordering::SeqCst) { Ok(()) } else { - bail!(error::ErrorKind::ConnectionClosed) + Err(error::Error::ConnectionClosed) } } diff --git a/ws/src/session.rs b/ws/src/session.rs index a5c28d9e9..7e3198a3a 100644 --- a/ws/src/session.rs +++ b/ws/src/session.rs @@ -254,7 +254,7 @@ impl> ws::Handler for Session { if let Some(result) = response { let res = out.send(result); match res { - Err(error::Error(error::ErrorKind::ConnectionClosed, _)) => { + Err(error::Error::ConnectionClosed) => { active_lock.store(false, atomic::Ordering::SeqCst); }, Err(e) => { From faed8cb57a0c0783bf0820789fbd1860759706df Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 28 Feb 2019 17:28:14 +0000 Subject: [PATCH 2/2] ws: document Error --- ws/src/error.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ws/src/error.rs b/ws/src/error.rs index 34b122ebb..3b01a7b24 100644 --- a/ws/src/error.rs +++ b/ws/src/error.rs @@ -1,16 +1,19 @@ -#![allow(missing_docs)] - use std::{io, error, fmt, result}; use crate::ws; +/// WebSockets Server Error #[derive(Debug)] pub enum Error { + /// Io Error Io(io::Error), + /// WebSockets Error WsError(ws::Error), + /// Connection Closed ConnectionClosed, } +/// WebSockets Server Result pub type Result = result::Result; impl fmt::Display for Error {