Skip to content

Commit fbab213

Browse files
Ben Striegelbstrie
Ben Striegel
authored andcommitted
refactor(ilp-cli): improve error message
1 parent 91614a8 commit fbab213

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

crates/ilp-cli/src/interpreter.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub enum Error {
1414
ProtocolErr(String),
1515
// Foreign errors
1616
#[error("Error sending HTTP request: {0}")]
17-
ClientErr(#[from] reqwest::Error),
17+
SendErr(#[from] reqwest::Error),
18+
#[error("Error receving HTTP response from testnet: {0}")]
19+
TestnetErr(reqwest::Error),
1820
#[error("Error altering URL scheme")]
1921
SchemeErr(()), // TODO: should be part of UrlError, see https://github.com/servo/rust-url/issues/299
2022
#[error("Error parsing URL: {0}")]
@@ -83,7 +85,7 @@ impl NodeClient<'_> {
8385
.get(&format!("{}/accounts/{}/balance", self.url, user))
8486
.bearer_auth(auth)
8587
.send()
86-
.map_err(Error::ClientErr)
88+
.map_err(Error::SendErr)
8789
}
8890

8991
// POST /accounts
@@ -94,7 +96,7 @@ impl NodeClient<'_> {
9496
.bearer_auth(auth)
9597
.json(&args)
9698
.send()
97-
.map_err(Error::ClientErr)
99+
.map_err(Error::SendErr)
98100
}
99101

100102
// PUT /accounts/:username
@@ -105,7 +107,7 @@ impl NodeClient<'_> {
105107
.bearer_auth(auth)
106108
.json(&args)
107109
.send()
108-
.map_err(Error::ClientErr)
110+
.map_err(Error::SendErr)
109111
}
110112

111113
// DELETE /accounts/:username
@@ -115,7 +117,7 @@ impl NodeClient<'_> {
115117
.delete(&format!("{}/accounts/{}", self.url, args["username"]))
116118
.bearer_auth(auth)
117119
.send()
118-
.map_err(Error::ClientErr)
120+
.map_err(Error::SendErr)
119121
}
120122

121123
// WebSocket /accounts/:username/payments/incoming
@@ -157,7 +159,7 @@ impl NodeClient<'_> {
157159
.get(&format!("{}/accounts/{}", self.url, args["username"]))
158160
.bearer_auth(auth)
159161
.send()
160-
.map_err(Error::ClientErr)
162+
.map_err(Error::SendErr)
161163
}
162164

163165
// GET /accounts
@@ -167,7 +169,7 @@ impl NodeClient<'_> {
167169
.get(&format!("{}/accounts", self.url))
168170
.bearer_auth(auth)
169171
.send()
170-
.map_err(Error::ClientErr)
172+
.map_err(Error::SendErr)
171173
}
172174

173175
// PUT /accounts/:username/settings
@@ -179,7 +181,7 @@ impl NodeClient<'_> {
179181
.bearer_auth(auth)
180182
.json(&args)
181183
.send()
182-
.map_err(Error::ClientErr)
184+
.map_err(Error::SendErr)
183185
}
184186

185187
// POST /accounts/:username/payments
@@ -191,15 +193,15 @@ impl NodeClient<'_> {
191193
.bearer_auth(&format!("{}:{}", user, auth))
192194
.json(&args)
193195
.send()
194-
.map_err(Error::ClientErr)
196+
.map_err(Error::SendErr)
195197
}
196198

197199
// GET /rates
198200
fn get_rates(&self, _matches: &ArgMatches) -> Result<Response, Error> {
199201
self.client
200202
.get(&format!("{}/rates", self.url))
201203
.send()
202-
.map_err(Error::ClientErr)
204+
.map_err(Error::SendErr)
203205
}
204206

205207
// PUT /rates
@@ -210,15 +212,15 @@ impl NodeClient<'_> {
210212
.bearer_auth(auth)
211213
.json(&rate_pairs)
212214
.send()
213-
.map_err(Error::ClientErr)
215+
.map_err(Error::SendErr)
214216
}
215217

216218
// GET /routes
217219
fn get_routes(&self, _matches: &ArgMatches) -> Result<Response, Error> {
218220
self.client
219221
.get(&format!("{}/routes", self.url))
220222
.send()
221-
.map_err(Error::ClientErr)
223+
.map_err(Error::SendErr)
222224
}
223225

224226
// PUT /routes/static/:prefix
@@ -229,7 +231,7 @@ impl NodeClient<'_> {
229231
.bearer_auth(auth)
230232
.body(args["destination"].to_string())
231233
.send()
232-
.map_err(Error::ClientErr)
234+
.map_err(Error::SendErr)
233235
}
234236

235237
// PUT routes/static
@@ -240,7 +242,7 @@ impl NodeClient<'_> {
240242
.bearer_auth(auth)
241243
.json(&route_pairs)
242244
.send()
243-
.map_err(Error::ClientErr)
245+
.map_err(Error::SendErr)
244246
}
245247

246248
// PUT /settlement/engines
@@ -251,15 +253,15 @@ impl NodeClient<'_> {
251253
.bearer_auth(auth)
252254
.json(&engine_pairs)
253255
.send()
254-
.map_err(Error::ClientErr)
256+
.map_err(Error::SendErr)
255257
}
256258

257259
// GET /
258260
fn get_root(&self, _matches: &ArgMatches) -> Result<Response, Error> {
259261
self.client
260262
.get(&format!("{}/", self.url))
261263
.send()
262-
.map_err(Error::ClientErr)
264+
.map_err(Error::SendErr)
263265
}
264266

265267
/*
@@ -280,10 +282,9 @@ impl NodeClient<'_> {
280282
let foreign_args: XpringResponse = self
281283
.client
282284
.get(&format!("https://xpring.io/api/accounts/{}", asset))
283-
.send()
284-
.map_err(Error::ClientErr)?
285+
.send()?
285286
.json()
286-
.map_err(Error::ClientErr)?;
287+
.map_err(Error::TestnetErr)?;
287288
let mut args = HashMap::new();
288289
let token = format!("{}:{}", foreign_args.username, foreign_args.passkey);
289290
args.insert("ilp_over_http_url", foreign_args.http_endpoint);
@@ -311,7 +312,7 @@ impl NodeClient<'_> {
311312
http::Response::builder().body(token).unwrap(), // infallible unwrap
312313
))
313314
} else {
314-
result.map_err(Error::ClientErr)
315+
result.map_err(Error::SendErr)
315316
}
316317
}
317318
}

crates/ilp-cli/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ mod interface_tests {
206206
Err(e) => panic!("Failed to parse command `{}`: {}", example, e),
207207
Ok(matches) => match run(&matches) {
208208
// Because these are interface tests, not integration tests, network errors are expected
209-
Ok(_) | Err(Error::ClientErr(_)) | Err(Error::WebsocketErr(_)) => (),
209+
Ok(_)
210+
| Err(Error::SendErr(_))
211+
| Err(Error::WebsocketErr(_))
212+
| Err(Error::TestnetErr(_)) => (),
210213
Err(e) => panic!("Unexpected interpreter failure: {}", e),
211214
},
212215
}

0 commit comments

Comments
 (0)