Skip to content

Commit d06defa

Browse files
refactor(cli): define error messages using the failure crate
Signed-off-by: Kevin W Matthews <[email protected]>
1 parent 2a86eae commit d06defa

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
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.

crates/ilp-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repository = "https://github.com/interledger-rs/interledger-rs"
99

1010
[dependencies]
1111
clap = { version = "2.33.0", default-features = false }
12+
failure = { version = "0.1.6", default-features = false }
1213
http = { version = "0.1.18", default-features = false }
1314
reqwest = { version = "0.9.21", default-features = false, features = ["default-tls"] }
1415
serde = { version = "1.0.101", default-features = false }

crates/ilp-cli/src/interpreter.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
use clap::ArgMatches;
2+
use failure::Fail;
23
use http;
34
use reqwest::{self, Client, Response};
45
use std::{borrow::Cow, collections::HashMap};
56
use tungstenite::{connect, handshake::client::Request};
67
use url::Url;
78

8-
#[derive(Debug)]
9+
#[derive(Debug, Fail)]
910
pub enum Error {
11+
#[fail(display = "Usage error")]
1012
UsageErr(&'static str),
13+
#[fail(display = "Error sending request: {}", _0)]
1114
ClientErr(reqwest::Error),
15+
#[fail(display = "Error receiving response: {}", _0)]
1216
ResponseErr(String),
17+
#[fail(display = "Error parsing URL protocol: {}", _0)]
1318
ProtocolErr(&'static str),
19+
#[fail(display = "Error parsing URL: {}", _0)]
1420
UrlErr(url::ParseError),
21+
#[fail(display = "Error connecting to WebSocket host: {}", _0)]
1522
WebsocketErr(tungstenite::error::Error),
1623
}
1724

@@ -134,7 +141,7 @@ impl NodeClient<'_> {
134141
let scheme = match url.scheme() {
135142
"http" => Ok("ws"),
136143
"https" => Ok("wss"),
137-
_ => Err(Error::ProtocolErr("Unexpected URL protocol")),
144+
_ => Err(Error::ProtocolErr("Unexpected protocol")),
138145
}?;
139146

140147
// The scheme has already been sanitized so this should always succeed

crates/ilp-cli/src/main.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,8 @@ pub fn main() {
1919
// help text for an arbitrary subcommand, but this works just the same.
2020
app.get_matches_from(s.split(' '));
2121
}
22-
Err(interpreter::Error::ClientErr(e)) => {
23-
eprintln!("ILP CLI error: failed to send request: {}", e);
24-
exit(1);
25-
}
26-
Err(interpreter::Error::ResponseErr(e)) => {
27-
eprintln!("ILP CLI error: invalid response: {}", e);
28-
exit(1);
29-
}
30-
Err(interpreter::Error::ProtocolErr(e)) => {
31-
eprintln!("ILP CLI error: failed to send request: {}", e);
32-
exit(1);
33-
}
34-
Err(interpreter::Error::UrlErr(e)) => {
35-
eprintln!("ILP CLI error: failed to send request: {}", e);
36-
exit(1);
37-
}
38-
Err(interpreter::Error::WebsocketErr(e)) => {
39-
eprintln!("ILP CLI error: Could not connect to WebSocket host: {}", e);
22+
Err(e) => {
23+
eprintln!("ILP CLI error: {}", e);
4024
exit(1);
4125
}
4226
Ok(mut response) => match response.text() {

0 commit comments

Comments
 (0)