|
15 | 15 |
|
16 | 16 | use anyhow::*; |
17 | 17 | use litesvm::types::TransactionMetadata; |
18 | | -use solana_sdk::instruction::InstructionError; |
| 18 | +use solana_sdk::{instruction::InstructionError, program_error::ProgramError}; |
19 | 19 | use testsvm_core::prelude::*; |
20 | 20 |
|
21 | 21 | /// Provides assertion methods for failed transactions. |
@@ -128,6 +128,30 @@ impl TXErrorAssertions { |
128 | 128 | } |
129 | 129 | } |
130 | 130 |
|
| 131 | + /// Asserts that the transaction failed with a specific program error. |
| 132 | + pub fn with_program_error<T: Into<ProgramError>>(&self, err: T) -> Result<()> { |
| 133 | + let program_error: ProgramError = err.into(); |
| 134 | + match self.error.metadata.err.clone() { |
| 135 | + solana_sdk::transaction::TransactionError::InstructionError(_, err) => { |
| 136 | + let result_program_error: ProgramError = err.try_into()?; |
| 137 | + if result_program_error == program_error { |
| 138 | + Ok(()) |
| 139 | + } else { |
| 140 | + Err(anyhow!( |
| 141 | + "Expected custom program error {}, but got '{}'", |
| 142 | + program_error, |
| 143 | + result_program_error |
| 144 | + )) |
| 145 | + } |
| 146 | + } |
| 147 | + _ => Err(anyhow!( |
| 148 | + "Expected custom program error {}, but got instruction error '{}'", |
| 149 | + program_error, |
| 150 | + self.error.metadata.err.to_string() |
| 151 | + )), |
| 152 | + } |
| 153 | + } |
| 154 | + |
131 | 155 | /// Returns the underlying transaction error for custom assertions. |
132 | 156 | pub fn error(&self) -> &TXError { |
133 | 157 | &self.error |
@@ -270,7 +294,13 @@ impl TXResultAssertions for TXResult { |
270 | 294 | } |
271 | 295 |
|
272 | 296 | fn succeeds(self) -> Result<TXSuccessAssertions> { |
273 | | - let metadata = self?; |
274 | | - Ok(TXSuccessAssertions { metadata }) |
| 297 | + match self { |
| 298 | + Result::Ok(metadata) => Ok(TXSuccessAssertions { metadata }), |
| 299 | + Result::Err(e) => { |
| 300 | + e.print_error(); |
| 301 | + e.address_book.print_all(); |
| 302 | + Err(anyhow::anyhow!("Unexpected failed transaction: {}", e)) |
| 303 | + } |
| 304 | + } |
275 | 305 | } |
276 | 306 | } |
0 commit comments