turdle_client/
error_reporter.rs

1use anchor_client::solana_client::client_error::ClientErrorKind;
2use anchor_client::solana_client::rpc_request::RpcError::RpcResponseError;
3use anchor_client::solana_client::rpc_request::RpcResponseErrorData;
4use anchor_client::solana_client::rpc_response::RpcSimulateTransactionResult;
5use anchor_client::ClientError;
6use anyhow::Error;
7
8trait ErrorReporter<T> {
9    fn report(error: &T);
10}
11
12impl ErrorReporter<ClientError> for ClientError {
13    fn report(error: &ClientError) {
14        if let ClientError::SolanaClientError(err) = error {
15            if let ClientErrorKind::RpcError(RpcResponseError {
16                data,
17                code,
18                message,
19            }) = err.kind()
20            {
21                let formatted_data = match data {
22                    RpcResponseErrorData::SendTransactionPreflightFailure(
23                        RpcSimulateTransactionResult {
24                            logs: Some(logs), ..
25                        },
26                    ) => logs.join("\n   "),
27                    _ => "".to_string(),
28                };
29                println!("RpcResponseError [{code}]: {message}\n{formatted_data}");
30            }
31        }
32    }
33}
34
35pub fn report_error(error: &Error) {
36    if let Some(err) = error.downcast_ref::<ClientError>() {
37        ClientError::report(err);
38    };
39}