light_client/rpc/
errors.rs

1use solana_banks_client::BanksClientError;
2use solana_client::client_error::ClientError;
3use solana_program::instruction::InstructionError;
4use solana_sdk::transaction::TransactionError;
5use std::io;
6use thiserror::Error;
7
8#[derive(Error, Debug)]
9pub enum RpcError {
10    #[error("BanksError: {0}")]
11    BanksError(#[from] BanksClientError),
12
13    #[error("TransactionError: {0}")]
14    TransactionError(#[from] TransactionError),
15
16    #[error("ClientError: {0}")]
17    ClientError(#[from] ClientError),
18
19    #[error("IoError: {0}")]
20    IoError(#[from] io::Error),
21
22    #[error("Error: `{0}`")]
23    CustomError(String),
24
25    #[error("Assert Rpc Error: {0}")]
26    AssertRpcError(String),
27
28    /// The chosen warp slot is not in the future, so warp is not performed
29    #[error("Warp slot not in the future")]
30    InvalidWarpSlot,
31}
32
33pub fn assert_rpc_error<T>(
34    result: Result<T, RpcError>,
35    i: u8,
36    expected_error_code: u32,
37) -> Result<(), RpcError> {
38    match result {
39        Err(RpcError::TransactionError(TransactionError::InstructionError(
40            index,
41            InstructionError::Custom(error_code),
42        ))) if index != i => Err(RpcError::AssertRpcError(
43            format!(
44                "Expected error code: {}, got: {} error: {}",
45                expected_error_code,
46                error_code,
47                unsafe { result.unwrap_err_unchecked() }
48            )
49            .to_string(),
50        )),
51        Err(RpcError::TransactionError(TransactionError::InstructionError(
52            index,
53            InstructionError::Custom(error_code),
54        ))) if index == i && error_code == expected_error_code => Ok(()),
55
56        Err(RpcError::TransactionError(TransactionError::InstructionError(
57            0,
58            InstructionError::ProgramFailedToComplete,
59        ))) => Ok(()),
60        Err(e) => Err(RpcError::AssertRpcError(format!(
61            "Unexpected error type: {:?}",
62            e
63        ))),
64        _ => Err(RpcError::AssertRpcError(String::from(
65            "Unexpected error type",
66        ))),
67    }
68}