Skip to content

Commit 8f921ed

Browse files
tomusdrwdebris
authored andcommitted
Introduce error_chain (#215)
* Use error-chain for ws server. * Error chain for http. * Get rid of error wrapper for minihttp. * Add jsonrpc_core::Result. * Fix crate use. * Add comments. * Make ws error more abstract. * Remove explicit message type.
1 parent 17c6272 commit 8f921ed

File tree

29 files changed

+152
-248
lines changed

29 files changed

+152
-248
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ extern crate jsonrpc_core;
7171
#[macro_use]
7272
extern crate jsonrpc_macros;
7373

74-
use jsonrpc_core::Error;
74+
use jsonrpc_core::Result;
7575

7676
build_rpc_trait! {
7777
pub trait Rpc {
7878
/// Adds two numbers and returns a result
7979
#[rpc(name = "add")]
80-
fn add(&self, u64, u64) -> Result<u64, Error>;
80+
fn add(&self, u64, u64) -> Result<u64>;
8181
}
8282
}
8383

8484
pub struct RpcImpl;
8585
impl Rpc for RpcImpl {
86-
fn add(&self, a: u64, b: u64) -> Result<u64, Error> {
86+
fn add(&self, a: u64, b: u64) -> Result<u64> {
8787
Ok(a + b)
8888
}
8989
}

core/src/calls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub trait RpcMethodSimple: Send + Sync + 'static {
1818
/// Asynchronous Method with Metadata
1919
pub trait RpcMethod<T: Metadata>: Send + Sync + 'static {
2020
/// Call method
21-
fn call(&self, params: Params, meta: T) -> BoxFuture<Value, Error>;
21+
fn call(&self, params: Params, meta: T) -> BoxFuture<Value>;
2222
}
2323

2424
/// Notification
@@ -69,7 +69,7 @@ impl<F: Send + Sync + 'static, X: Send + 'static, T, I> RpcMethod<T> for F where
6969
I: IntoFuture<Item = Value, Error = Error, Future = X>,
7070
X: Future<Item = Value, Error = Error>,
7171
{
72-
fn call(&self, params: Params, meta: T) -> BoxFuture<Value, Error> {
72+
fn call(&self, params: Params, meta: T) -> BoxFuture<Value> {
7373
Box::new(self(params, meta).into_future())
7474
}
7575
}

core/src/io.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use calls::{RemoteProcedure, Metadata, RpcMethodSimple, RpcMethod, RpcNotificati
99
use middleware::{self, Middleware};
1010
use types::{Params, Error, ErrorCode, Version};
1111
use types::{Request, Response, Call, Output};
12-
use BoxFuture;
1312

1413
/// A type representing middleware or RPC response before serialization.
15-
pub type FutureResponse = BoxFuture<Option<Response>, ()>;
14+
pub type FutureResponse = Box<Future<Item=Option<Response>, Error=()> + Send>;
1615

1716
/// A type representing future string response.
1817
pub type FutureResult<F> = future::Map<
@@ -22,7 +21,7 @@ pub type FutureResult<F> = future::Map<
2221

2322
/// A type representing a result of a single method call.
2423
pub type FutureOutput = future::Either<
25-
BoxFuture<Option<Output>, ()>,
24+
Box<Future<Item=Option<Output>, Error=()> + Send>,
2625
future::FutureResult<Option<Output>, ()>,
2726
>;
2827

core/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ mod middleware;
3939
pub mod types;
4040

4141
/// A `Future` trait object.
42-
pub type BoxFuture<T, E> = Box<futures::Future<Item = T, Error = E> + Send>;
42+
pub type BoxFuture<T> = Box<futures::Future<Item = T, Error = Error> + Send>;
43+
44+
/// A Result type.
45+
pub type Result<T> = ::std::result::Result<T, Error>;
4346

4447
pub use calls::{RemoteProcedure, Metadata, RpcMethodSimple, RpcMethod, RpcNotificationSimple, RpcNotification};
4548
pub use io::{Compatibility, IoHandler, MetaIoHandler, FutureResponse, FutureResult};

core/src/middleware.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use calls::Metadata;
44
use types::{Request, Response};
55
use futures::Future;
6-
use BoxFuture;
76

87
/// RPC middleware
98
pub trait Middleware<M: Metadata>: Send + Sync + 'static {
@@ -22,7 +21,7 @@ pub trait Middleware<M: Metadata>: Send + Sync + 'static {
2221
#[derive(Default)]
2322
pub struct Noop;
2423
impl<M: Metadata> Middleware<M> for Noop {
25-
type Future = BoxFuture<Option<Response>, ()>;
24+
type Future = Box<Future<Item=Option<Response>, Error=()> + Send>;
2625

2726
fn on_request<F, X>(&self, request: Request, meta: M, process: F) -> Self::Future where
2827
F: FnOnce(Request, M) -> X + Send,

core/src/types/response.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use serde::de::{Deserialize, Deserializer, Error as DeError};
33
use serde::ser::{Serialize, Serializer};
44
use serde_json::value::from_value;
55
use super::{Id, Value, Error, ErrorCode, Version};
6+
use {Result as CoreResult};
67

78
/// Successful response
89
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@@ -39,7 +40,7 @@ pub enum Output {
3940

4041
impl Output {
4142
/// Creates new output given `Result`, `Id` and `Version`.
42-
pub fn from(result: Result<Value, Error>, id: Id, jsonrpc: Option<Version>) -> Self {
43+
pub fn from(result: CoreResult<Value>, id: Id, jsonrpc: Option<Version>) -> Self {
4344
match result {
4445
Ok(result) => Output::Success(Success {
4546
id: id,
@@ -80,9 +81,9 @@ impl Output {
8081
}
8182
}
8283

83-
impl From<Output> for Result<Value, Error> {
84+
impl From<Output> for CoreResult<Value> {
8485
/// Convert into a result. Will be `Ok` if it is a `Success` and `Err` if `Failure`.
85-
fn from(output: Output) -> Result<Value, Error> {
86+
fn from(output: Output) -> CoreResult<Value> {
8687
match output {
8788
Output::Success(s) => Ok(s.result),
8889
Output::Failure(f) => Err(f.error),

http/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ keywords = ["jsonrpc", "json-rpc", "json", "rpc", "server"]
1010
documentation = "https://paritytech.github.io/jsonrpc/jsonrpc_http_server/index.html"
1111

1212
[dependencies]
13-
log = "0.3"
14-
net2 = "0.2"
13+
hyper = "0.11"
1514
jsonrpc-core = { version = "8.0", path = "../core" }
1615
jsonrpc-server-utils = { version = "8.0", path = "../server-utils" }
17-
hyper = "0.11"
16+
log = "0.3"
17+
net2 = "0.2"
1818
unicase = "2.0"
1919

2020
[badges]

http/src/handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hyper::{self, mime, server, Method};
77
use hyper::header::{self, Headers};
88
use unicase::Ascii;
99

10-
use jsonrpc::{self as core, FutureResult, BoxFuture, Metadata, Middleware, NoopMiddleware};
10+
use jsonrpc::{self as core, FutureResult, Metadata, Middleware, NoopMiddleware};
1111
use jsonrpc::futures::{Future, Poll, Async, Stream, future};
1212
use jsonrpc::serde_json;
1313
use response::Response;
@@ -90,7 +90,7 @@ impl<M: Metadata, S: Middleware<M>> server::Service for ServerHandler<M, S> {
9090
pub enum Handler<M: Metadata, S: Middleware<M>> {
9191
Rpc(RpcHandler<M, S>),
9292
Error(Option<Response>),
93-
Middleware(BoxFuture<server::Response, hyper::Error>),
93+
Middleware(Box<Future<Item=server::Response, Error=hyper::Error> + Send>),
9494
}
9595

9696
impl<M: Metadata, S: Middleware<M>> Future for Handler<M, S> {

http/src/lib.rs

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@
1919
2020
#![warn(missing_docs)]
2121

22-
#[macro_use] extern crate log;
2322
extern crate unicase;
2423
extern crate jsonrpc_core as jsonrpc;
2524
extern crate jsonrpc_server_utils as server_utils;
2625
extern crate net2;
2726

2827
pub extern crate hyper;
2928

30-
mod response;
29+
#[macro_use]
30+
extern crate log;
31+
3132
mod handler;
33+
mod response;
3234
mod utils;
3335
#[cfg(test)]
3436
mod tests;
3537

36-
use std::{fmt, io};
38+
use std::io;
3739
use std::sync::{mpsc, Arc};
3840
use std::net::SocketAddr;
3941

4042
use hyper::server;
41-
use jsonrpc::{BoxFuture, MetaIoHandler};
43+
use jsonrpc::MetaIoHandler;
4244
use jsonrpc::futures::{self, Future, Stream};
4345
use jsonrpc::futures::sync::oneshot;
4446
use server_utils::reactor::{Remote, UninitializedRemote};
@@ -50,55 +52,6 @@ pub use handler::ServerHandler;
5052
pub use utils::{is_host_allowed, cors_header, CorsHeader};
5153
pub use response::Response;
5254

53-
/// Result of starting the Server.
54-
pub type ServerResult = Result<Server, Error>;
55-
56-
/// RPC Server startup error.
57-
#[derive(Debug)]
58-
pub enum Error {
59-
/// IO Error
60-
Io(std::io::Error),
61-
/// Other Error (hyper)
62-
Other(hyper::error::Error),
63-
}
64-
65-
impl From<std::io::Error> for Error {
66-
fn from(err: std::io::Error) -> Self {
67-
Error::Io(err)
68-
}
69-
}
70-
71-
impl From<hyper::error::Error> for Error {
72-
fn from(err: hyper::error::Error) -> Self {
73-
match err {
74-
hyper::error::Error::Io(e) => Error::Io(e),
75-
e => Error::Other(e)
76-
}
77-
}
78-
}
79-
80-
impl fmt::Display for Error {
81-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82-
match *self {
83-
Error::Io(ref e) => e.fmt(f),
84-
Error::Other(ref e) => e.fmt(f),
85-
}
86-
}
87-
}
88-
89-
impl ::std::error::Error for Error {
90-
fn description(&self) -> &str {
91-
"Starting the JSON-RPC HTTP server failed"
92-
}
93-
94-
fn cause(&self) -> Option<&::std::error::Error> {
95-
Some(match *self {
96-
Error::Io(ref e) => e,
97-
Error::Other(ref e) => e,
98-
})
99-
}
100-
}
101-
10255
/// Action undertaken by a middleware.
10356
pub enum RequestMiddlewareAction {
10457
/// Proceed with standard RPC handling
@@ -114,7 +67,7 @@ pub enum RequestMiddlewareAction {
11467
/// Should standard hosts validation be performed?
11568
should_validate_hosts: bool,
11669
/// a future for server response
117-
response: BoxFuture<server::Response, hyper::Error>,
70+
response: Box<Future<Item=server::Response, Error=hyper::Error> + Send>,
11871
}
11972
}
12073

@@ -338,7 +291,7 @@ impl<M: jsonrpc::Metadata, S: jsonrpc::Middleware<M>> ServerBuilder<M, S> {
338291
}
339292

340293
/// Start this JSON-RPC HTTP server trying to bind to specified `SocketAddr`.
341-
pub fn start_http(self, addr: &SocketAddr) -> ServerResult {
294+
pub fn start_http(self, addr: &SocketAddr) -> io::Result<Server> {
342295
let cors_domains = self.cors_domains;
343296
let request_middleware = self.request_middleware;
344297
let allowed_hosts = self.allowed_hosts;

macros/examples/meta-macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate jsonrpc_tcp_server;
55

66
use std::collections::BTreeMap;
77

8-
use jsonrpc_core::{futures, MetaIoHandler, Metadata, Error, Value};
8+
use jsonrpc_core::{futures, MetaIoHandler, Metadata, Error, Value, Result};
99
use jsonrpc_core::futures::future::FutureResult;
1010

1111
#[derive(Clone, Default)]
@@ -18,11 +18,11 @@ build_rpc_trait! {
1818

1919
/// Adds two numbers and returns a result
2020
#[rpc(name = "add")]
21-
fn add(&self, u64, u64) -> Result<u64, Error>;
21+
fn add(&self, u64, u64) -> Result<u64>;
2222

2323
/// Multiplies two numbers. Second number is optional.
2424
#[rpc(name = "mul")]
25-
fn mul(&self, u64, jsonrpc_macros::Trailing<u64>) -> Result<u64, Error>;
25+
fn mul(&self, u64, jsonrpc_macros::Trailing<u64>) -> Result<u64>;
2626

2727
/// Performs asynchronous operation
2828
#[rpc(name = "callAsync")]
@@ -38,11 +38,11 @@ struct RpcImpl;
3838
impl Rpc for RpcImpl {
3939
type Metadata = Meta;
4040

41-
fn add(&self, a: u64, b: u64) -> Result<u64, Error> {
41+
fn add(&self, a: u64, b: u64) -> Result<u64> {
4242
Ok(a + b)
4343
}
4444

45-
fn mul(&self, a: u64, b: jsonrpc_macros::Trailing<u64>) -> Result<u64, Error> {
45+
fn mul(&self, a: u64, b: jsonrpc_macros::Trailing<u64>) -> Result<u64> {
4646
Ok(a * b.unwrap_or(1))
4747
}
4848

0 commit comments

Comments
 (0)