Skip to content

Commit e05dc3c

Browse files
authored
chore: make errors more informative (#15)
1 parent 0fa8d86 commit e05dc3c

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

crates/rpc-tester/src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod report;
77

88
/// Equality rpc test error
99
enum TestError {
10-
Diff { rpc1: serde_json::Value, rpc2: serde_json::Value },
10+
Diff { rpc1: serde_json::Value, rpc2: serde_json::Value, args: Option<String> },
1111
Rpc1Err(String),
1212
Rpc2Err(String),
1313
}
@@ -21,54 +21,61 @@ type MethodName = String;
2121
/// Provider macro that boxes all method future results.
2222
#[macro_export]
2323
macro_rules! rpc {
24-
($self:expr, $method:ident $(, $args:expr )* ) => {
24+
($self:expr, $method:ident $(, $args:expr )* ) => {{
25+
let args_str = Some(format!("{}", [$(format!("{:?}", $args)),*].join(", ")));
2526
Box::pin($self.test_rpc_call(
2627
stringify!($method),
28+
args_str,
2729
move |provider: &P| {
2830
provider.$method( $( $args.clone(), )*)
2931
}
3032
)) as Pin<Box<dyn Future<Output = (MethodName, Result<(), TestError>)> + Send>>
31-
};
33+
}};
3234
}
3335

3436
/// Provider macro to call methods that return `RpcWithBlock` and box the future results.
3537
#[macro_export]
3638
macro_rules! rpc_with_block {
37-
($self:expr, $method:ident $(, $args:expr )*; $blockid:expr) => {
39+
($self:expr, $method:ident $(, $args:expr )*; $blockid:expr) => {{
40+
let args_str = Some(format!("{}, block_id: {:?}", [$(format!("{:?}", $args)),*].join(", "), $blockid));
3841
Box::pin($self.test_rpc_call(
3942
stringify!($method),
43+
args_str,
4044
move |provider: &P| {
4145
provider.$method( $( $args.clone(), )*).block_id($blockid).into_future()
4246
}
4347
)) as Pin<Box<dyn Future<Output = (MethodName, Result<(), TestError>)> + Send>>
44-
};
48+
}};
4549
}
4650

4751
/// Macro to call the `get_logs` rpc method and box the future result.
4852
#[macro_export]
4953
macro_rules! get_logs {
50-
($self:expr, $arg:expr) => {
54+
($self:expr, $arg:expr) => {{
55+
let args_str = Some(format!("{:?}", $arg));
5156
Box::pin(async move {
5257
let filter = $arg.clone();
5358
$self
54-
.test_rpc_call(stringify!(get_logs), move |provider: &P| {
59+
.test_rpc_call(stringify!(get_logs), args_str, move |provider: &P| {
5560
let filter = filter.clone();
5661
async move { provider.get_logs(&filter).await }
5762
})
5863
.await
5964
}) as Pin<Box<dyn Future<Output = (MethodName, Result<(), TestError>)> + Send>>
60-
};
65+
}};
6166
}
6267

6368
/// Macro to create raw request and box the future result.
6469
#[macro_export]
6570
macro_rules! rpc_raw {
66-
($self:expr, $method:ident, $ret:ident $(, $args:expr )* ) => {
71+
($self:expr, $method:ident, $ret:ident $(, $args:expr )* ) => {{
72+
let args_str = Some(format!("{}", [$(format!("{:?}", $args)),*].join(", ")));
6773
Box::pin($self.test_rpc_call(
6874
stringify!($method),
75+
args_str,
6976
move |provider: &P| {
7077
provider.raw_request::<_, $ret>(stringify!($method).into(), $( $args.clone(), )*)
7178
}
7279
)) as Pin<Box<dyn Future<Output = (MethodName, Result<(), TestError>)> + Send>>
73-
};
80+
}};
7481
}

crates/rpc-tester/src/report.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@ use serde_json::Value;
1010
pub(crate) fn report(results_by_block: ReportResults) -> eyre::Result<()> {
1111
let mut passed = true;
1212
println!("\n--- RPC Method Test Results ---");
13+
println!(" (expected = rpc2, actual = rpc1)\n");
1314

1415
for (title, results) in results_by_block {
1516
let mut passed_title = true;
1617

1718
for (name, result) in results {
1819
match result {
1920
Ok(_) => {}
20-
Err(TestError::Diff { rpc1, rpc2 }) => {
21+
Err(TestError::Diff { rpc1, rpc2, args }) => {
2122
// While results are different, we only report it as error if __RPC1__ is
2223
// missing/mismatching any element against RPC2.
2324
if let Some(diffs) = verify_missing_or_mismatch(rpc1, rpc2) {
2425
if passed_title {
2526
passed_title = false;
2627
println!("\n{title} ❌");
2728
}
28-
println!(" {name}: ❌ Failure ");
29+
println!(" {name}: ❌ Failure");
30+
if let Some(args) = args {
31+
println!(" args: {args}");
32+
}
2933
println!("{diffs}");
3034
}
3135
}
@@ -70,7 +74,8 @@ fn verify_missing_or_mismatch(rpc1: Value, rpc2: Value) -> Option<String> {
7074
.downcast_ref::<&str>()
7175
.map(|s| s.to_string())
7276
.unwrap_or_else(|| err.downcast_ref::<String>().cloned().expect("should"))
73-
.replace("actual", "rpc1");
77+
.replace("actual", "actual (rpc1)")
78+
.replace("expected", "expected (rpc2)");
7479
return Some(err_msg);
7580
}
7681
None

crates/rpc-tester/src/tester.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ where
241241
async fn test_rpc_call<'a, F, Fut, T, E>(
242242
&'a self,
243243
name: &str,
244+
args: Option<String>,
244245
method_call: F,
245246
) -> (MethodName, Result<(), TestError>)
246247
where
@@ -271,6 +272,7 @@ where
271272
Err(TestError::Diff {
272273
rpc1: serde_json::to_value(&rpc1).expect("should json"),
273274
rpc2: serde_json::to_value(&rpc2).expect("should json"),
275+
args,
274276
})
275277
}
276278
}

0 commit comments

Comments
 (0)