light_program_test/utils/
assert.rs

1use light_client::rpc::RpcError;
2use solana_banks_client::BanksClientError;
3use solana_instruction::error::InstructionError;
4use solana_sdk::transaction::TransactionError;
5
6#[allow(clippy::result_large_err)]
7pub fn assert_rpc_error<T>(
8    result: Result<T, RpcError>,
9    index_instruction: u8,
10    expected_error_code: u32,
11) -> Result<(), RpcError> {
12    match result {
13        Err(RpcError::TransactionError(TransactionError::InstructionError(
14            index,
15            InstructionError::Custom(error_code),
16        ))) if index != index_instruction => Err(RpcError::AssertRpcError(
17            format!(
18                "Expected error code: {}, got: {} error: {}",
19                expected_error_code,
20                error_code,
21                unsafe { result.unwrap_err_unchecked() }
22            )
23            .to_string(),
24        )),
25        Err(RpcError::BanksError(BanksClientError::TransactionError(
26            TransactionError::InstructionError(index, InstructionError::Custom(error_code)),
27        ))) if index != index_instruction => Err(RpcError::AssertRpcError(
28            format!(
29                "Expected error code: {}, got: {} error: {}",
30                expected_error_code,
31                error_code,
32                unsafe { result.unwrap_err_unchecked() }
33            )
34            .to_string(),
35        )),
36        Err(RpcError::BanksError(BanksClientError::TransactionError(
37            TransactionError::InstructionError(index, InstructionError::Custom(error_code)),
38        ))) if index == index_instruction && error_code == expected_error_code => Ok(()),
39        Err(RpcError::TransactionError(TransactionError::InstructionError(
40            index,
41            InstructionError::Custom(error_code),
42        ))) if index == index_instruction && error_code == expected_error_code => Ok(()),
43
44        Err(RpcError::TransactionError(TransactionError::InstructionError(
45            0,
46            InstructionError::ProgramFailedToComplete,
47        ))) => Ok(()),
48        Err(e) => Err(RpcError::AssertRpcError(format!(
49            "Unexpected error type: {:?}",
50            e
51        ))),
52        _ => Err(RpcError::AssertRpcError(String::from(
53            "Unexpected error type",
54        ))),
55    }
56}