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

Optional custom error for constraints? #509

Closed
fanatid opened this issue Jul 10, 2021 · 10 comments
Closed

Optional custom error for constraints? #509

fanatid opened this issue Jul 10, 2021 · 10 comments
Labels
help wanted Extra attention is needed lang

Comments

@fanatid
Copy link
Contributor

fanatid commented Jul 10, 2021

Sometimes it's can be useful to have a special error code for constraints because this can provide a clear error message. Possible syntax:

#[account(constraint = <expression> ? MyError)]

Maybe not limited by constraint only?

@armaniferrante
Copy link
Member

Agreed. I think this would be useful for any constraint, particularly those where there can be more than one, e.g., has_one, so that one can differentiate between the scenarios.

I like the ?. Another possibility would be to use a ;.

#[account(constraint = <expression>; MyError)]

Though I don't have a preference. Tag @Henry-E who I previously discussed this with as well.

@fanatid
Copy link
Contributor Author

fanatid commented Jul 10, 2021

In the case of ; how few constraints will look like?

#[account(
  constraint = <expression>; MyError,
  constraint = <expression>
)]

vs

#[account(
  constraint = <expression> ? MyError,
  constraint = <expression>
)]

but in general, I do not care about symbols if a custom error will be possible.

@armaniferrante
Copy link
Member

armaniferrante commented Jul 10, 2021

Another one that I like in addition to ? is

#[account(
    constraint = <expression> ! MyError,
    constraint = <expression>
)]

@acamill
Copy link
Contributor

acamill commented Oct 7, 2021

Was hoping for this. Voting for ! as it implies unwrapping/error, ? implies conditional (to me, coming from Swift).

Or possible to have something rustier like :
Constraint = (condition).map_err(Error)? Not very rust savvy

@Henry-E
Copy link

Henry-E commented Oct 7, 2021 via email

@armaniferrante
Copy link
Member

The custom error would be nice for GUIs perhaps.

@acamill
Copy link
Contributor

acamill commented Oct 8, 2021

The custom error would be nice for GUIs perhaps.

Even for debugging constraintful contextes. Currently offloading so pre checks in "access-control" even if it's not acl related

@rav3nh0lm
Copy link

Yes would be amazing, I am trying to debug some code, and it's incredibly exhausting trying to understand where the error originated from (i.e. from one of the macros)

@acamill
Copy link
Contributor

acamill commented Oct 12, 2021

I am currently dodging this by using access_control even if it's not really about rights. Doing some pre check there (most of the constraints that are a bit freestyle) so that I can have custom errors.

Would it make sense for clarity sake to have another function similar to access_control but named something like parameter_validation, or that should just be in the main body of the IX code (I like to have purely the execution logic there)?

Long term most of the arbitrary constraints should be about mint/authority, all these field related to the underlaying "native" types. For the custom stuff it might make more sense to be offloaded somewhere else as in these "parameter_validation" that are purely business logic.

@armaniferrante
Copy link
Member

Closed by #905.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed lang
Projects
None yet
Development

No branches or pull requests

5 participants