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

Error redesign #1462

Merged
merged 119 commits into from
Feb 20, 2022
Merged

Error redesign #1462

merged 119 commits into from
Feb 20, 2022

Conversation

paul-schaaf
Copy link
Contributor

@paul-schaaf paul-schaaf commented Feb 17, 2022

closes #798

Changes:

  • adds Error enum (AnchorError or ProgramErrorWithOrigin)
  • adds type Result<T> = Result<T, Error> thats to be used instead of ProgramResult
  • adds error! macro that creates AnchorError with source info and wraps it inside an Error
  • adds err! macro that expands into Err(error!(#error_code))
  • adds source! macro that can be used to enrich ProgramErrors with Source information
  • renames #[error] to #[error_code]
  • adjust ts library so it can parse custom errors from real tx instead of just simulated ones

lang/src/lib.rs Outdated Show resolved Hide resolved
lang/src/common.rs Outdated Show resolved Hide resolved
impl std::convert::From<Error> for anchor_lang::solana_program::program_error::ProgramError {
fn from(e: Error) -> anchor_lang::solana_program::program_error::ProgramError {
match e {
Error::AnchorError(AnchorError {
Copy link
Member

Choose a reason for hiding this comment

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

No need to change. But FYI this can be rewritten as

AnchorError {
  error_code_number,
  ..
  }

Copy link
Member

@armaniferrante armaniferrante left a comment

Choose a reason for hiding this comment

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

Looks good to go after the tests pass once more.

@armaniferrante armaniferrante merged commit 4059682 into coral-xyz:master Feb 20, 2022
@archseer
Copy link
Contributor

  • adds err! macro that expands into Err(error!(#error_code))

And what does error! expand to? Why can't this be Err(MyError::A.into())?

@paul-schaaf
Copy link
Contributor Author

the full expansion can be seen in lang/attribute/error

.into() doesnt work because we want to return an error with file and line info and that info should be the same line as the error! call

@paul-schaaf
Copy link
Contributor Author

paul-schaaf commented Feb 21, 2022

Err(MyError::A.into()) this also works now but it wont include source information

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.

Automatically log errors when program fails
3 participants