Skip to content

Commit 0916361

Browse files
authored
Feature/error equality (#1544)
1 parent 269200b commit 0916361

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The minor version will be incremented upon a breaking change and the patch versi
1515
* cli: Add `--program-keypair` to `anchor deploy` ([#1786](https://github.com/project-serum/anchor/pull/1786)).
1616
* spl: Add more derived traits to `TokenAccount` to `Mint` ([#1818](https://github.com/project-serum/anchor/pull/1818)).
1717
* cli: Add compilation optimizations to cli template ([#1807](https://github.com/project-serum/anchor/pull/1807)).
18+
* lang: Add `PartialEq` and `Eq` for `anchor_lang::Error` ([#1544](https://github.com/project-serum/anchor/pull/1544)).
1819

1920
### Fixes
2021

lang/src/error.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub enum ErrorCode {
195195
Deprecated = 5000,
196196
}
197197

198-
#[derive(Debug)]
198+
#[derive(Debug, PartialEq, Eq)]
199199
pub enum Error {
200200
AnchorError(AnchorError),
201201
ProgramError(ProgramErrorWithOrigin),
@@ -302,6 +302,14 @@ pub struct ProgramErrorWithOrigin {
302302
pub compared_values: Option<ComparedValues>,
303303
}
304304

305+
// Two ProgramErrors are equal when they have the same error code
306+
impl PartialEq for ProgramErrorWithOrigin {
307+
fn eq(&self, other: &Self) -> bool {
308+
self.program_error == other.program_error
309+
}
310+
}
311+
impl Eq for ProgramErrorWithOrigin {}
312+
305313
impl Display for ProgramErrorWithOrigin {
306314
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
307315
Display::fmt(&self.program_error, f)
@@ -458,6 +466,15 @@ impl Display for AnchorError {
458466
}
459467
}
460468

469+
/// Two `AnchorError`s are equal when they have the same error code
470+
impl PartialEq for AnchorError {
471+
fn eq(&self, other: &Self) -> bool {
472+
self.error_code_number == other.error_code_number
473+
}
474+
}
475+
476+
impl Eq for AnchorError {}
477+
461478
impl std::convert::From<Error> for anchor_lang::solana_program::program_error::ProgramError {
462479
fn from(e: Error) -> anchor_lang::solana_program::program_error::ProgramError {
463480
match e {

0 commit comments

Comments
 (0)