Skip to content

Commit b165fac

Browse files
authored
Remove failure dependency. (#579)
* Remove failure dependency. * Remove needless format!. * cargo fmt --all
1 parent 8e7ddc9 commit b165fac

File tree

7 files changed

+62
-52
lines changed

7 files changed

+62
-52
lines changed

core-client/transports/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ipc = [
3636
arbitrary_precision = ["serde_json/arbitrary_precision", "jsonrpc-core/arbitrary_precision"]
3737

3838
[dependencies]
39-
failure = "0.1"
39+
derive_more = "0.99"
4040
futures = { version = "0.3", features = [ "compat" ] }
4141
jsonrpc-core = { version = "15.0", path = "../../core" }
4242
jsonrpc-pubsub = { version = "15.0", path = "../../pubsub" }

core-client/transports/src/lib.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
#![deny(missing_docs)]
44

5-
use failure::{format_err, Fail};
65
use jsonrpc_core::futures::channel::{mpsc, oneshot};
76
use jsonrpc_core::futures::{
87
self,
@@ -22,20 +21,34 @@ pub mod transports;
2221
mod logger;
2322

2423
/// The errors returned by the client.
25-
#[derive(Debug, Fail)]
24+
#[derive(Debug, derive_more::Display)]
2625
pub enum RpcError {
2726
/// An error returned by the server.
28-
#[fail(display = "Server returned rpc error {}", _0)]
27+
#[display(fmt = "Server returned rpc error {}", _0)]
2928
JsonRpcError(Error),
3029
/// Failure to parse server response.
31-
#[fail(display = "Failed to parse server response as {}: {}", _0, _1)]
32-
ParseError(String, failure::Error),
30+
#[display(fmt = "Failed to parse server response as {}: {}", _0, _1)]
31+
ParseError(String, Box<dyn std::error::Error + Send>),
3332
/// Request timed out.
34-
#[fail(display = "Request timed out")]
33+
#[display(fmt = "Request timed out")]
3534
Timeout,
35+
/// A general client error.
36+
#[display(fmt = "Client error: {}", _0)]
37+
Client(String),
3638
/// Not rpc specific errors.
37-
#[fail(display = "{}", _0)]
38-
Other(failure::Error),
39+
#[display(fmt = "{}", _0)]
40+
Other(Box<dyn std::error::Error + Send>),
41+
}
42+
43+
impl std::error::Error for RpcError {
44+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45+
match *self {
46+
Self::JsonRpcError(ref e) => Some(e),
47+
Self::ParseError(_, ref e) => Some(&**e),
48+
Self::Other(ref e) => Some(&**e),
49+
_ => None,
50+
}
51+
}
3952
}
4053

4154
impl From<Error> for RpcError {
@@ -162,7 +175,7 @@ impl<T: DeserializeOwned + Unpin + 'static> Stream for TypedSubscriptionStream<T
162175
match result {
163176
Some(Ok(value)) => Some(
164177
serde_json::from_value::<T>(value)
165-
.map_err(|error| RpcError::ParseError(self.returns.into(), error.into())),
178+
.map_err(|error| RpcError::ParseError(self.returns.into(), Box::new(error))),
166179
),
167180
None => None,
168181
Some(Err(err)) => Some(Err(err.into())),
@@ -192,9 +205,9 @@ impl RawClient {
192205
};
193206
let result = self.0.send(msg.into());
194207
async move {
195-
let () = result.map_err(|e| RpcError::Other(e.into()))?;
208+
let () = result.map_err(|e| RpcError::Other(Box::new(e)))?;
196209

197-
receiver.await.map_err(|e| RpcError::Other(e.into()))?
210+
receiver.await.map_err(|e| RpcError::Other(Box::new(e)))?
198211
}
199212
}
200213

@@ -206,7 +219,7 @@ impl RawClient {
206219
};
207220
match self.0.send(msg.into()) {
208221
Ok(()) => Ok(()),
209-
Err(error) => Err(RpcError::Other(error.into())),
222+
Err(error) => Err(RpcError::Other(Box::new(error))),
210223
}
211224
}
212225

@@ -232,7 +245,7 @@ impl RawClient {
232245
self.0
233246
.send(msg.into())
234247
.map(|()| receiver)
235-
.map_err(|e| RpcError::Other(e.into()))
248+
.map_err(|e| RpcError::Other(Box::new(e)))
236249
}
237250
}
238251

@@ -266,9 +279,9 @@ impl TypedClient {
266279
Value::Array(vec) => Ok(Params::Array(vec)),
267280
Value::Null => Ok(Params::None),
268281
Value::Object(map) => Ok(Params::Map(map)),
269-
_ => Err(RpcError::Other(format_err!(
270-
"RPC params should serialize to a JSON array, JSON object or null"
271-
))),
282+
_ => Err(RpcError::Client(
283+
"RPC params should serialize to a JSON array, JSON object or null".into(),
284+
)),
272285
};
273286
let result = params.map(|params| self.0.call_method(method, params));
274287

@@ -277,7 +290,7 @@ impl TypedClient {
277290

278291
log::debug!("response: {:?}", value);
279292

280-
serde_json::from_value::<R>(value).map_err(|error| RpcError::ParseError(returns, error.into()))
293+
serde_json::from_value::<R>(value).map_err(|error| RpcError::ParseError(returns, Box::new(error)))
281294
}
282295
}
283296

@@ -289,9 +302,9 @@ impl TypedClient {
289302
Value::Array(vec) => Params::Array(vec),
290303
Value::Null => Params::None,
291304
_ => {
292-
return Err(RpcError::Other(format_err!(
293-
"RPC params should serialize to a JSON array, or null"
294-
)))
305+
return Err(RpcError::Client(
306+
"RPC params should serialize to a JSON array, or null".into(),
307+
))
295308
}
296309
};
297310

@@ -314,9 +327,9 @@ impl TypedClient {
314327
Value::Array(vec) => Params::Array(vec),
315328
Value::Null => Params::None,
316329
_ => {
317-
return Err(RpcError::Other(format_err!(
318-
"RPC params should serialize to a JSON array, or null"
319-
)))
330+
return Err(RpcError::Client(
331+
"RPC params should serialize to a JSON array, or null".into(),
332+
))
320333
}
321334
};
322335

core-client/transports/src/transports/duplex.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Duplex transport
22
3-
use failure::format_err;
43
use futures::channel::{mpsc, oneshot};
54
use futures::{
65
task::{Context, Poll},
@@ -195,7 +194,7 @@ where
195194
// It's a regular Req-Res call, so just answer.
196195
Some(PendingRequest::Call(tx)) => {
197196
tx.send(result)
198-
.map_err(|_| RpcError::Other(format_err!("oneshot channel closed")))?;
197+
.map_err(|_| RpcError::Client("oneshot channel closed".into()))?;
199198
continue;
200199
}
201200
// It was a subscription request,
@@ -219,11 +218,9 @@ where
219218
);
220219
}
221220
} else {
222-
let err = RpcError::Other(format_err!(
221+
let err = RpcError::Client(format!(
223222
"Subscription {:?} ({:?}) rejected: {:?}",
224-
id,
225-
method,
226-
result,
223+
id, method, result,
227224
));
228225

229226
if subscription.channel.unbounded_send(result).is_err() {
@@ -276,7 +273,7 @@ where
276273
// Writes queued messages to sink.
277274
log::debug!("handle outgoing");
278275
loop {
279-
let err = || Err(RpcError::Other(failure::format_err!("closing")));
276+
let err = || Err(RpcError::Client("closing".into()));
280277
match self.sink.as_mut().poll_ready(cx) {
281278
Poll::Ready(Ok(())) => {}
282279
Poll::Ready(Err(_)) => return err().into(),

core-client/transports/src/transports/http.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
use super::RequestBuilder;
66
use crate::{RpcChannel, RpcError, RpcMessage, RpcResult};
7-
use failure::format_err;
87
use futures::{Future, FutureExt, StreamExt, TryFutureExt, TryStreamExt};
98
use hyper::{http, rt, Client, Request, Uri};
109

@@ -39,13 +38,13 @@ fn do_connect(url: &str) -> impl Future<Output = RpcResult<RpcChannel>> {
3938
let max_parallel = 8;
4039
let url: Uri = match url.parse() {
4140
Ok(url) => url,
42-
Err(e) => return ready(Err(RpcError::Other(e.into()))),
41+
Err(e) => return ready(Err(RpcError::Other(Box::new(e)))),
4342
};
4443

4544
#[cfg(feature = "tls")]
4645
let connector = match hyper_tls::HttpsConnector::new(4) {
4746
Ok(connector) => connector,
48-
Err(e) => return ready(Err(RpcError::Other(e.into()))),
47+
Err(e) => return ready(Err(RpcError::Other(Box::new(e)))),
4948
};
5049
#[cfg(feature = "tls")]
5150
let client = Client::builder().build::<_, hyper::Body>(connector);
@@ -97,16 +96,16 @@ fn do_connect(url: &str) -> impl Future<Output = RpcResult<RpcChannel>> {
9796
let future = match result {
9897
Ok(ref res) if !res.status().is_success() => {
9998
log::trace!("http result status {}", res.status());
100-
A(future::err(RpcError::Other(format_err!(
99+
A(future::err(RpcError::Client(format!(
101100
"Unexpected response status code: {}",
102101
res.status()
103102
))))
104103
}
105104
Ok(res) => B(res
106105
.into_body()
107-
.map_err(|e| RpcError::ParseError(e.to_string(), e.into()))
106+
.map_err(|e| RpcError::ParseError(e.to_string(), Box::new(e)))
108107
.concat2()),
109-
Err(err) => A(future::err(RpcError::Other(err.into()))),
108+
Err(err) => A(future::err(RpcError::Other(Box::new(err)))),
110109
};
111110
future.then(|result| {
112111
if let Some(sender) = sender {

core-client/transports/src/transports/local.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ where
8181

8282
fn send_response(&mut self) -> Result<(), RpcError> {
8383
if let Buffered::Response(r) = std::mem::replace(&mut self.buffered, Buffered::None) {
84-
self.queue.0.start_send(r).map_err(|e| RpcError::Other(e.into()))?;
84+
self.queue.0.start_send(r).map_err(|e| RpcError::Other(Box::new(e)))?;
8585
}
8686
Ok(())
8787
}
@@ -97,7 +97,7 @@ where
9797
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
9898
futures::ready!(self.poll_buffered(cx))?;
9999
futures::ready!(self.queue.0.poll_ready(cx))
100-
.map_err(|e| RpcError::Other(e.into()))
100+
.map_err(|e| RpcError::Other(Box::new(e)))
101101
.into()
102102
}
103103

@@ -110,13 +110,13 @@ where
110110
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
111111
futures::ready!(self.poll_buffered(cx))?;
112112
futures::ready!(self.queue.0.poll_flush_unpin(cx))
113-
.map_err(|e| RpcError::Other(e.into()))
113+
.map_err(|e| RpcError::Other(Box::new(e)))
114114
.into()
115115
}
116116

117117
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
118118
futures::ready!(self.queue.0.poll_close_unpin(cx))
119-
.map_err(|e| RpcError::Other(e.into()))
119+
.map_err(|e| RpcError::Other(Box::new(e)))
120120
.into()
121121
}
122122
}

core-client/transports/src/transports/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn parse_response(
8282
response: &str,
8383
) -> Result<(Id, Result<Value, RpcError>, Option<String>, Option<SubscriptionId>), RpcError> {
8484
jsonrpc_core::serde_from_str::<ClientResponse>(response)
85-
.map_err(|e| RpcError::ParseError(e.to_string(), e.into()))
85+
.map_err(|e| RpcError::ParseError(e.to_string(), Box::new(e)))
8686
.map(|response| {
8787
let id = response.id().unwrap_or(Id::Null);
8888
let sid = response.subscription_id();

core-client/transports/src/transports/ws.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! JSON-RPC websocket client implementation.
22
use crate::{RpcChannel, RpcError};
3-
use failure::Error;
43
use futures01::prelude::*;
54
use log::info;
65
use std::collections::VecDeque;
@@ -11,11 +10,11 @@ use websocket::{ClientBuilder, OwnedMessage};
1110
/// Uses an unbuffered channel to queue outgoing rpc messages.
1211
///
1312
/// Returns `Err` if the `url` is invalid.
14-
pub fn try_connect<T>(url: &str) -> Result<impl Future<Item = T, Error = RpcError>, Error>
13+
pub fn try_connect<T>(url: &str) -> Result<impl Future<Item = T, Error = RpcError>, RpcError>
1514
where
1615
T: From<RpcChannel>,
1716
{
18-
let client_builder = ClientBuilder::new(url)?;
17+
let client_builder = ClientBuilder::new(url).map_err(|e| RpcError::Other(Box::new(e)))?;
1918
Ok(do_connect(client_builder))
2019
}
2120

@@ -54,7 +53,7 @@ where
5453
tokio::spawn(rpc_client);
5554
sender.into()
5655
})
57-
.map_err(|error| RpcError::Other(error.into()))
56+
.map_err(|error| RpcError::Other(Box::new(error)))
5857
}
5958

6059
struct WebsocketClient<TSink, TStream> {
@@ -67,7 +66,7 @@ impl<TSink, TStream, TError> WebsocketClient<TSink, TStream>
6766
where
6867
TSink: Sink<SinkItem = OwnedMessage, SinkError = TError>,
6968
TStream: Stream<Item = OwnedMessage, Error = TError>,
70-
TError: Into<Error>,
69+
TError: std::error::Error + Send + 'static,
7170
{
7271
pub fn new(sink: TSink, stream: TStream) -> Self {
7372
Self {
@@ -82,7 +81,7 @@ impl<TSink, TStream, TError> Sink for WebsocketClient<TSink, TStream>
8281
where
8382
TSink: Sink<SinkItem = OwnedMessage, SinkError = TError>,
8483
TStream: Stream<Item = OwnedMessage, Error = TError>,
85-
TError: Into<Error>,
84+
TError: std::error::Error + Send + 'static,
8685
{
8786
type SinkItem = String;
8887
type SinkError = RpcError;
@@ -101,20 +100,22 @@ where
101100
self.queue.push_front(request);
102101
break;
103102
}
104-
Err(error) => return Err(RpcError::Other(error.into())),
103+
Err(error) => return Err(RpcError::Other(Box::new(error))),
105104
},
106105
None => break,
107106
}
108107
}
109-
self.sink.poll_complete().map_err(|error| RpcError::Other(error.into()))
108+
self.sink
109+
.poll_complete()
110+
.map_err(|error| RpcError::Other(Box::new(error)))
110111
}
111112
}
112113

113114
impl<TSink, TStream, TError> Stream for WebsocketClient<TSink, TStream>
114115
where
115116
TSink: Sink<SinkItem = OwnedMessage, SinkError = TError>,
116117
TStream: Stream<Item = OwnedMessage, Error = TError>,
117-
TError: Into<Error>,
118+
TError: std::error::Error + Send + 'static,
118119
{
119120
type Item = String;
120121
type Error = RpcError;
@@ -134,7 +135,7 @@ where
134135
return Ok(Async::Ready(None));
135136
}
136137
Ok(Async::NotReady) => return Ok(Async::NotReady),
137-
Err(error) => return Err(RpcError::Other(error.into())),
138+
Err(error) => return Err(RpcError::Other(Box::new(error))),
138139
}
139140
}
140141
}

0 commit comments

Comments
 (0)