1

I am able to see my custom error messages (defined on chain) when I call the [anchor] program using javascript client node client.js:

error: {
    errorCode: { code: 'MEOW', number: 6003 },
    errorMessage: 'This account a custom err message that I have written',
    comparedValues: undefined,
    origin: { file: 'programs/helloworld/src/lib.rs', line: 46 }
  },

the errorCode and errorMessage are especially helpful. How can I print those fields using Rust? Right now, I am only getting

Error: RPC response error -32002: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x1774 [5 log messages]

Caused by:
    0: RPC response error -32002: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x1774 [5 log messages]
    1: RPC response error -32002: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x1774 [5 log messages]

my rust client code follows this template:

fn main() -> Result<()> {
    call_instruction()?,
    Ok(())
}

fn call_instruction() -> Result <()> {
    let program = ...;

    let signature = program
    .request()
    .accounts(program::accounts::Test {
        user: program.payer(),
    }).args(program::instruction::Testing {}).send()?;

    println!("Transaction signature: {:?}",signature);
    Ok(())
}

In terminal, I have tried setting env variable RUSTLOG=solana_client=debug before running cargo run, but nothing happens :(

2
  • 1
    please update the question to provide the code you're using to print the errors
    – trent.sol
    Commented Aug 2, 2022 at 18:02
  • I have included the rust client (shortened for readability). I run it with cargo run
    – coco
    Commented Aug 2, 2022 at 20:17

2 Answers 2

2

If you wanted to print in the context of a test, you might do something like:

#[test]
fn some_test() {
 let result = call_instruction();
 if result.is_err(){
  //whatever
 }
}

And here you could extract the error (result.err()) if you wanted to print the log or code.

More commonly you have some errorCode values specified in your errors.rs, you are emitting these with return err!(ErrorCode::Whatever); when something goes wrong, and this gets caught and printed by the caller.

1
  • Hi! yes, my on chain code looks like return err!(MyError::CustomErrorName);, but I am not sure what you mean by "gets caught and printed by the caller." :( my terminal prints the "CustomErrorName" when I am using javascript, but for Rust, I can only see Transaction simulation failed: Error processing Instruction 0: custom program error: 0x1772 [5 log messages]
    – coco
    Commented Aug 2, 2022 at 21:18
0

RpcClient is returning an error type of "solana_rpc_client_api::client_error::Error". This Error type contains a member field called "kind" that can be matched against. For example, to map this to anyhow::Error:

    use solana_rpc_client_api::client_error::ErrorKind;
    // ...
    rpc_client
      .send_and_confirm_transaction_with_spinner(&transaction)
      .await.map_err(|e| {
        match e.kind {
        ErrorKind::RpcError(rpc_err) => {
          println!("ERROR: {rpc_err:?}");
          Error::new(rpc_err)
        }
          // match other types
          _ => { Error::new(e) }
        }
    })

More specifically, the log messages you are looking for are likely buried down under: ErrorKind/RpcError/RpcResponseError/RpcResponseErrorData/SendTransactionPreflightFailure/RpcSimulateTransactionResult/logs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.