Skip to content

Commit ac197bd

Browse files
authored
ws: replace error-chain with vanilla Error impl (paritytech#399)
* ws: replace error-chain with vanilla Error impl * ws: document Error
1 parent 1296a12 commit ac197bd

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

ws/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ repository = "https://github.com/paritytech/jsonrpc"
1010
version = "10.1.0"
1111

1212
[dependencies]
13-
error-chain = "0.12"
1413
jsonrpc-core = { version = "10.1", path = "../core" }
1514
jsonrpc-server-utils = { version = "10.1", path = "../server-utils" }
1615
log = "0.4"

ws/src/error.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
1-
#![allow(missing_docs)]
2-
3-
use std::io;
1+
use std::{io, error, fmt, result};
42

53
use crate::ws;
64

7-
error_chain! {
8-
foreign_links {
9-
Io(io::Error);
5+
/// WebSockets Server Error
6+
#[derive(Debug)]
7+
pub enum Error {
8+
/// Io Error
9+
Io(io::Error),
10+
/// WebSockets Error
11+
WsError(ws::Error),
12+
/// Connection Closed
13+
ConnectionClosed,
14+
}
15+
16+
/// WebSockets Server Result
17+
pub type Result<T> = result::Result<T, Error>;
18+
19+
impl fmt::Display for Error {
20+
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
21+
match self {
22+
Error::ConnectionClosed => write!(f, "Action on closed connection."),
23+
Error::WsError(err) => write!(f, "WebSockets Error: {}", err),
24+
Error::Io(err) => write!(f, "Io Error: {}", err),
25+
}
1026
}
27+
}
1128

12-
errors {
13-
/// Attempted action on closed connection.
14-
ConnectionClosed {
15-
description("connection is closed"),
16-
display("Action on closed connection."),
29+
impl error::Error for Error {
30+
fn source(&self) -> Option<&(error::Error + 'static)> {
31+
match self {
32+
Error::Io(io) => Some(io),
33+
Error::WsError(ws) => Some(ws),
34+
Error::ConnectionClosed => None,
1735
}
1836
}
1937
}
2038

39+
impl From<io::Error> for Error {
40+
fn from(err: io::Error) -> Self {
41+
Error::Io(err)
42+
}
43+
}
44+
2145
impl From<ws::Error> for Error {
2246
fn from(err: ws::Error) -> Self {
2347
match err.kind {
24-
ws::ErrorKind::Io(e) => e.into(),
25-
_ => Error::with_chain(err, "WebSockets Error"),
48+
ws::ErrorKind::Io(err) => Error::Io(err),
49+
_ => Error::WsError(err),
2650
}
2751
}
2852
}

ws/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use jsonrpc_server_utils as server_utils;
77
pub use ws;
88
pub use jsonrpc_core;
99

10-
#[macro_use]
11-
extern crate error_chain;
1210
#[macro_use]
1311
extern crate log;
1412

@@ -22,7 +20,7 @@ mod tests;
2220

2321
use jsonrpc_core as core;
2422

25-
pub use self::error::{Error, ErrorKind, Result};
23+
pub use self::error::{Error, Result};
2624
pub use self::metadata::{RequestContext, MetaExtractor, NoopExtractor};
2725
pub use self::session::{RequestMiddleware, MiddlewareAction};
2826
pub use self::server::{CloseHandle, Server};

ws/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Sender {
2929
if self.active.load(atomic::Ordering::SeqCst) {
3030
Ok(())
3131
} else {
32-
bail!(error::ErrorKind::ConnectionClosed)
32+
Err(error::Error::ConnectionClosed)
3333
}
3434
}
3535

ws/src/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<M: core::Metadata, S: core::Middleware<M>> ws::Handler for Session<M, S> {
254254
if let Some(result) = response {
255255
let res = out.send(result);
256256
match res {
257-
Err(error::Error(error::ErrorKind::ConnectionClosed, _)) => {
257+
Err(error::Error::ConnectionClosed) => {
258258
active_lock.store(false, atomic::Ordering::SeqCst);
259259
},
260260
Err(e) => {

0 commit comments

Comments
 (0)