Skip to content

Commit 2a86eae

Browse files
fix(cli): do not panic with invalid user input
Extend the cli's Error enum and implement the From trait to allow the try operator to be used. Signed-off-by: Kevin W Matthews <[email protected]>
1 parent 6213595 commit 2a86eae

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

crates/ilp-cli/src/interpreter.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ pub enum Error {
1010
UsageErr(&'static str),
1111
ClientErr(reqwest::Error),
1212
ResponseErr(String),
13+
ProtocolErr(&'static str),
14+
UrlErr(url::ParseError),
15+
WebsocketErr(tungstenite::error::Error),
16+
}
17+
18+
impl From<url::ParseError> for Error {
19+
fn from(error: url::ParseError) -> Self {
20+
Error::UrlErr(error)
21+
}
22+
}
23+
24+
impl From<tungstenite::error::Error> for Error {
25+
fn from(error: tungstenite::error::Error) -> Self {
26+
Error::WebsocketErr(error)
27+
}
1328
}
1429

1530
pub fn run(matches: &ArgMatches) -> Result<Response, Error> {
@@ -114,23 +129,24 @@ impl NodeClient<'_> {
114129
let mut url = Url::parse(&format!(
115130
"{}/accounts/{}/payments/incoming",
116131
self.url, args["username"]
117-
))
118-
.expect("Could not parse URL");
132+
))?;
133+
134+
let scheme = match url.scheme() {
135+
"http" => Ok("ws"),
136+
"https" => Ok("wss"),
137+
_ => Err(Error::ProtocolErr("Unexpected URL protocol")),
138+
}?;
119139

120-
url.set_scheme(match url.scheme() {
121-
"http" => "ws",
122-
"https" => "wss",
123-
_ => panic!("Unexpected URL protocol"),
124-
})
125-
.expect("Could not alter URL scheme");
140+
// The scheme has already been sanitized so this should always succeed
141+
url.set_scheme(scheme).expect("Could not alter URL scheme");
126142

127143
let mut request: Request = url.into();
128144
request.add_header(
129145
Cow::Borrowed("Authorization"),
130146
Cow::Owned(format!("Bearer {}", auth)),
131147
);
132148

133-
let (mut socket, _) = connect(request).expect("Could not connect to WebSocket host");
149+
let (mut socket, _) = connect(request)?;
134150
loop {
135151
let msg = socket
136152
.read_message()

crates/ilp-cli/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ pub fn main() {
2727
eprintln!("ILP CLI error: invalid response: {}", e);
2828
exit(1);
2929
}
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);
40+
exit(1);
41+
}
3042
Ok(mut response) => match response.text() {
3143
Err(e) => {
3244
eprintln!("ILP CLI error: Failed to parse HTTP response: {}", e);

0 commit comments

Comments
 (0)