Skip to content

Commit 487dcb9

Browse files
authored
added error_chain to grpc mock (#1045)
1 parent bdf85e3 commit 487dcb9

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jormungandr-integration-tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ lazy_static = "1.3"
3535
mktemp = "0.4.0"
3636
regex = "1.1"
3737
custom_error = "1.7"
38+
error-chain = "0.12"
3839
jormungandr = { path = "../jormungandr" }
3940
jcli = { path = "../jcli" }
4041

jormungandr-integration-tests/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ extern crate lazy_static;
55
#[macro_use]
66
extern crate slog;
77

8+
#[macro_use(error_chain)]
9+
extern crate error_chain;
10+
811
#[cfg(test)]
912
pub mod jcli;
1013
#[cfg(test)]

jormungandr-integration-tests/src/mock/client.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ extern crate hex;
77
extern crate protobuf;
88

99
use crate::mock::{
10-
grpc::*,
11-
proto::{node::*, node_grpc::*},
10+
grpc::{ClientStubExt, Error as GrpcError, Metadata},
11+
proto::{
12+
node::{
13+
Block, BlockIds, Fragment, FragmentIds, HandshakeRequest, HandshakeResponse, Header,
14+
PullBlocksToTipRequest, PullHeadersRequest, PushHeadersResponse, TipRequest,
15+
UploadBlocksResponse,
16+
},
17+
node_grpc::{Node, NodeClient, NodeServer},
18+
},
1219
read_into,
1320
};
1421
use chain_core::property::FromStr;
@@ -42,6 +49,19 @@ macro_rules! response_to_err {
4249
}};
4350
}
4451

52+
error_chain! {
53+
errors {
54+
InvalidRequest (message: String) {
55+
display("request failed with message {}", message),
56+
}
57+
58+
InvalidAddressFormat (address: String) {
59+
display("could not parse address '{}'. HINT: accepted format example: /ip4/127.0.0.1/tcp/9000", address),
60+
}
61+
62+
}
63+
}
64+
4565
pub struct JormungandrClient {
4666
client: NodeClient,
4767
host: String,
@@ -55,6 +75,23 @@ impl Clone for JormungandrClient {
5575
}
5676

5777
impl JormungandrClient {
78+
pub fn from_address(address: &str) -> Result<Self> {
79+
let elements: Vec<&str> = address.split("/").collect();
80+
81+
let host = elements.get(2);
82+
let port = elements.get(4);
83+
84+
if host.is_none() || port.is_none() {
85+
return Err(ErrorKind::InvalidAddressFormat(address.to_owned()).into());
86+
}
87+
88+
let port: u16 = port
89+
.unwrap()
90+
.parse()
91+
.map_err(|_err| ErrorKind::InvalidAddressFormat(address.to_owned()))?;
92+
Ok(Self::new(host.unwrap(), port))
93+
}
94+
5895
pub fn new(host: &str, port: u16) -> Self {
5996
let client_conf = Default::default();
6097
let client = NodeClient::new_plain(host, port, client_conf).unwrap();
@@ -120,6 +157,7 @@ impl JormungandrClient {
120157
grpc::StreamingRequest::single(block),
121158
);
122159
resp.wait()
160+
.map_err(|err| ErrorKind::InvalidRequest(err.to_string()).into())
123161
}
124162

125163
pub fn pull_blocks_to_tip(&self, from: Hash) -> grpc::StreamingResponse<Block> {
@@ -158,6 +196,7 @@ impl JormungandrClient {
158196
grpc::StreamingRequest::single(header),
159197
);
160198
resp.wait()
199+
.map_err(|err| ErrorKind::InvalidRequest(err.to_string()).into())
161200
}
162201

163202
pub fn get_fragments(&self, ids: Vec<Hash>) -> grpc::StreamingResponse<Fragment> {

0 commit comments

Comments
 (0)