Note: the doc is generated by AI
This repository demonstrates a critical limitation in Solana programs: Cross-Program Invocation (CPI) errors cannot be recovered from. When a CPI call fails, the entire transaction is reverted, and there's no way to catch or handle the error within the calling program.
The test_system_cpi instruction attempts to transfer tokens with insufficient funds:
// This should fail with insufficient funds
let transfer_result = token::transfer(transfer_ctx, 2);
msg!("CODE DIDNT REACH HERE ????"); // This line never executes!
match transfer_result {
Ok(_) => { /* Never reached */ },
Err(_err) => { /* Never reached */ }
}Result: The msg!("CODE DIDNT REACH HERE ????") line never executes because the transaction reverts immediately when the CPI fails.
The test_custom_cpi instruction calls a program that always fails:
// This should always fail
let cpi_result = always_fail(cpi_ctx);
msg!("CODE DIDNT REACH HERE ????") // This line never executes!
match cpi_result {
Ok(_) => { /* Never reached */ },
Err(_err) => { /* Never reached */ }
}Result: Same behavior - the transaction reverts immediately, and no error handling code executes.
# Install dependencies
npm install
# Build the programs
anchor build
# Run tests
anchor testBoth tests will fail with errors, demonstrating that:
- The CPI calls fail as expected
- No code after the failed CPI call executes
- The entire transaction reverts