Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert_with_error! macro #749

Merged
merged 5 commits into from
Nov 2, 2022
Merged

Add assert_with_error! macro #749

merged 5 commits into from
Nov 2, 2022

Conversation

leighmcculloch
Copy link
Member

@leighmcculloch leighmcculloch commented Oct 31, 2022

What

Add assert_with_error! macro that works like assert! but uses panic_error!.

Why

I've noticed that reviewing examples written by the folks experimenting with Soroban that folks like to use assert!. We provide a panic_error! function for folks who like to panic on error, so that they can panic with an error code instead of a string. Given the patterns I'm seeing with assert! I think it's worth also providing same for error codes, and seeing if folks find error codes preferrable with the same assertion pattern.

@leighmcculloch leighmcculloch marked this pull request as ready for review October 31, 2022 05:12
///
/// See [`contracterror`] for how to define an error type.
#[macro_export]
macro_rules! assert_error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose the name assert_error because it parallels panic_error (which parallels panic_any). I don't love the name assert_error, because it reads more like we're asserting that an error should occur, rather than creating an assertion that will cause an error if false. I've gone with consistency, but open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a little misleading. You are not asserting that there is an error, rather the opposite. For near's sdk, they went with require, which I would advocate for.

Also I can't wait until macro's can be bound to types! env.require!(...) reads better to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I can't wait until macro's can be bound to types! env.require!(...) reads better to me.

Is this in development? Do you have a link to the tracking PR / RFC? I can't find it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@leighmcculloch leighmcculloch Nov 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graydon suggested assert_with_error or assert_or_error.

On it's own I think I prefer assert_or_error over require because it retains the assert vocab. i.e.:

  • assert_or_error
  • panic_error

If we wanted panic_error to also follow, then the _with_error variant would probably fit better, i.e.:

  • assert_with_error
  • panic_with_error.

Thoughts? cc @willemneal @graydon @paulbellamy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*_with_error makes sense to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up: #752

@leighmcculloch leighmcculloch changed the title Add assert_error! macro Add assert_with_error! macro Nov 2, 2022
@leighmcculloch leighmcculloch merged commit 959b0d0 into main Nov 2, 2022
@leighmcculloch leighmcculloch deleted the antiscians-arided branch November 2, 2022 00:40
leighmcculloch added a commit that referenced this pull request Nov 3, 2022
### What
Rename `panic_error!` to `panic_with_error!`.

### Why
For consistency with `assert_with_error!` introduced in #749.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants