Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ws: replace error-chain with vanilla Error impl #399

Merged
merged 2 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
50 changes: 37 additions & 13 deletions ws/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
#![allow(missing_docs)]

use std::io;
use std::{io, error, fmt, result};

use crate::ws;

error_chain! {
foreign_links {
Io(io::Error);
/// 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<T> = result::Result<T, Error>;

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<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}

impl From<ws::Error> 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),
}
}
}
4 changes: 1 addition & 3 deletions ws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion ws/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Sender {
if self.active.load(atomic::Ordering::SeqCst) {
Ok(())
} else {
bail!(error::ErrorKind::ConnectionClosed)
Err(error::Error::ConnectionClosed)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ws/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<M: core::Metadata, S: core::Middleware<M>> ws::Handler for Session<M, S> {
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) => {
Expand Down