Skip to content

Commit 4878951

Browse files
committed
Simplifies logic and gets rid of errors
Errors are not returned as JSON encoded (as I assumed), but raised as std::io::Error by rpc.callmethod. Therefore the logic for parsing errors is unnecessary here.
1 parent c30f4fb commit 4878951

File tree

1 file changed

+15
-41
lines changed

1 file changed

+15
-41
lines changed

lightning-block-sync/src/convert.rs

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -192,42 +192,31 @@ impl TryInto<Transaction> for JsonResponse {
192192
// result may or may not be signed (e.g. signrawtransactionwithwallet)
193193
serde_json::Value::Bool(x) => {
194194
if x == false {
195+
let reason = match &self.0["errors"][0]["error"] {
196+
serde_json::Value::String(x) => x.as_str(),
197+
_ => "Unknown error",
198+
};
199+
195200
return Err(std::io::Error::new(
196201
std::io::ErrorKind::InvalidData,
197-
"transaction couldn't be signed",
202+
format!("transaction couldn't be signed. {}", reason),
198203
));
199204
} else {
200205
hex_data
201206
}
202207
}
203-
// result if a complete transaction (e.g. getrawtranaction)
208+
// result is a complete transaction (e.g. getrawtranaction verbose)
204209
_ => hex_data,
205210
},
206-
207-
// result is an error
208-
_ => match &self.0["error"] {
209-
serde_json::Value::String(error_data) => {
210-
return Err(std::io::Error::new(
211-
std::io::ErrorKind::InvalidData,
212-
format!(
213-
"trying to construct the transaction returned an error: {}",
214-
error_data
215-
),
216-
))
217-
}
218-
_ => {
219-
return Err(std::io::Error::new(
211+
_ => return Err(std::io::Error::new(
220212
std::io::ErrorKind::InvalidData,
221-
"no hex nor error fields in the provided JSON object",
222-
))
223-
}
224-
},
213+
"expected JSON string",
214+
)),
225215
}
226216
} else {
227-
// result is plain text
217+
// result is plain text (e.g. getrawtransaction no verbose)
228218
match self.0.as_str() {
229219
Some(hex_tx) => hex_tx,
230-
231220
None => {
232221
return Err(std::io::Error::new(
233222
std::io::ErrorKind::InvalidData,
@@ -679,22 +668,7 @@ pub(crate) mod tests {
679668
assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
680669
assert_eq!(
681670
e.get_ref().unwrap().to_string(),
682-
"trying to construct the transaction returned an error: foo"
683-
);
684-
}
685-
Ok(_) => panic!("Expected error"),
686-
}
687-
}
688-
689-
#[test]
690-
fn into_tx_from_json_response_with_no_hex_nor_error_fields() {
691-
let response = JsonResponse(serde_json::json!({ "result": "foo" }));
692-
match TryInto::<Transaction>::try_into(response) {
693-
Err(e) => {
694-
assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
695-
assert_eq!(
696-
e.get_ref().unwrap().to_string(),
697-
"no hex nor error fields in the provided JSON object"
671+
"expected JSON string"
698672
);
699673
}
700674
Ok(_) => panic!("Expected error"),
@@ -707,9 +681,9 @@ pub(crate) mod tests {
707681
match TryInto::<Transaction>::try_into(response) {
708682
Err(e) => {
709683
assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
710-
assert_eq!(
711-
e.get_ref().unwrap().to_string(),
712-
"transaction couldn't be signed"
684+
assert!(
685+
e.get_ref().unwrap().to_string().contains(
686+
"transaction couldn't be signed")
713687
);
714688
}
715689
Ok(_) => panic!("Expected error"),

0 commit comments

Comments
 (0)