Skip to content

Commit b45bca9

Browse files
authored
Fix: broken error macros and remove confusing err macro (#277)
- Removed err macro (unnecessary and confusing with `error`​) - fixed error macros and added extra tests to make sure they all compile in all variants
1 parent 0223ae9 commit b45bca9

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed error macros and add tests (#277)
13+
14+
### Removed
15+
16+
- Removed `err` macro (#277)
17+
1018
## [0.26.0] - 2025-09-25
1119

1220
### Added

star_frame/src/account_set/single_set.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,13 @@ where
264264
match rent_lamports.cmp(&lamports) {
265265
Ordering::Equal => Ok(()),
266266
Ordering::Greater => {
267-
if lamports > 0 {
268-
err!(
269-
ProgramError::InsufficientFunds,
270-
"Tried to refund rent from {} but does not have enough lamports to cover rent",
271-
account.pubkey()
272-
)
273-
} else {
274-
Ok(())
275-
}
267+
ensure!(
268+
lamports > 0,
269+
ProgramError::InsufficientFunds,
270+
"Tried to refund rent from {} but does not have enough lamports to cover rent",
271+
account.pubkey()
272+
);
273+
Ok(())
276274
}
277275
Ordering::Less => {
278276
let transfer_amount = lamports - rent_lamports;

star_frame/src/errors.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ macro_rules! ensure_eq {
8383
let left = $left;
8484
let right = $right;
8585
if left != right {
86-
return $crate::err!($err, format!("expected {:?}, found {:?}", right, left)).into();
86+
return Err($crate::error!($err, "expected {right:?}, found {left:?}").into());
8787
}
8888
}};
8989

9090
($left:expr, $right:expr, $err:expr, $($ctx:tt)*) => {{
9191
if $left != $right {
92-
return $crate::err!($err, $($ctx)*).into();
92+
return Err($crate::error!($err, $($ctx)*).into());
9393
}
9494
}};
9595
}
@@ -101,17 +101,17 @@ macro_rules! ensure_eq {
101101
macro_rules! ensure_ne {
102102
($left:expr, $right:expr, $err:expr $(,)?) => {{
103103
let right = $right;
104+
let left = $left;
104105
if left == right {
105-
return $crate::err!(
106+
return Err($crate::error!(
106107
$err,
107-
format!("expected to not be {:?}, found {:?}", right, left)
108-
)
109-
.into();
108+
"expected to not be {right:?}, found {left:?}"
109+
).into());
110110
}
111111
}};
112112
($left:expr, $right:expr, $err:expr, $($ctx:tt)*) => {{
113113
if $left == $right {
114-
return $crate::err!($err, $($ctx)*).into();
114+
return Err($crate::error!($err, $($ctx)*).into());
115115
}
116116
}};
117117
}
@@ -121,7 +121,7 @@ macro_rules! ensure_ne {
121121
macro_rules! ensure {
122122
($cond:expr, $err:expr $(, $($ctx:tt)*)?) => {
123123
if !$cond {
124-
return Err($crate::error!($err, $($($ctx)*)*)).into();
124+
return Err($crate::error!($err, $($($ctx)*)*).into());
125125
}
126126
};
127127
}
@@ -130,15 +130,7 @@ macro_rules! ensure {
130130
#[macro_export]
131131
macro_rules! bail {
132132
($err:expr $(, $($ctx:tt)*)?) => {
133-
return $crate::err!($err, $($($ctx)*)*).into()
134-
};
135-
}
136-
137-
/// Construcs an [`Err<Error>`](Error)
138-
#[macro_export]
139-
macro_rules! err {
140-
($err:expr $(, $($ctx:tt)*)?) => {
141-
Err($crate::error!($err, $($($ctx)*)*))
133+
return Err($crate::error!($err, $($($ctx)*)*).into())
142134
};
143135
}
144136

@@ -589,7 +581,9 @@ mod tests {
589581
ensure!(true, ErrorCode::BorrowError, "Hello {}!", "world");
590582
let res: Result<(), Error> = (|| {
591583
ensure_eq!(0, 1, ProgramError::IllegalOwner, "Test {:?}", "aaa");
584+
ensure_eq!(0, 1, ProgramError::IllegalOwner);
592585
ensure_ne!(0, 1, ProgramError::IllegalOwner, "Test {}", "aaa");
586+
ensure_ne!(0, 1, ProgramError::IllegalOwner);
593587
Ok(())
594588
})();
595589

star_frame/src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use crate::{
3030
ClockExt, GetKeyFor as _, GetOptionalKeyFor as _, KeyFor, OptionalKeyFor, OptionalPubkey,
3131
PackedValue, SetKeyFor as _, UnitVal,
3232
},
33-
ensure, ensure_eq, ensure_ne, err, error,
33+
ensure, ensure_eq, ensure_ne, error,
3434
errors::{star_frame_error, Error, ErrorInfo as _},
3535
instruction::{
3636
star_frame_instruction, InstructionArgs, InstructionDiscriminant as _, InstructionSet,

0 commit comments

Comments
 (0)