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

lang: Clearer error when handling not initialized accounts #1024

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ incremented for features.

### Fixes

lang: Add `deprecated` attribute to `ProgramAccount` ([#1014](https://github.com/project-serum/anchor/pull/1014)).
* lang: Add `deprecated` attribute to `ProgramAccount` ([#1014](https://github.com/project-serum/anchor/pull/1014)).
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the whole CHANGELOG, you don't have single items without a "*" at the beginning. I thought it was a typo and corrected it. Boy Scout Rule 😅


### Features

* lang: Add `ErrorCode::AccountNotInitialized` error to separate the situation when the account has the wrong owner from when it does not exist (#[1024](https://github.com/project-serum/anchor/pull/1024))

## [0.18.2] - 2021-11-14

Expand Down
6 changes: 6 additions & 0 deletions lang/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> Account<'a, T
/// Deserializes the given `info` into a `Account`.
#[inline(never)]
pub fn try_from(info: &AccountInfo<'a>) -> Result<Account<'a, T>, ProgramError> {
if info.owner == &system_program::ID && info.lamports() == 0 {
return Err(ErrorCode::AccountNotInitialized.into());
}
if info.owner != &T::owner() {
return Err(ErrorCode::AccountNotProgramOwned.into());
}
Expand All @@ -34,6 +37,9 @@ impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> Account<'a, T
/// possible.
#[inline(never)]
pub fn try_from_unchecked(info: &AccountInfo<'a>) -> Result<Account<'a, T>, ProgramError> {
if info.owner == &system_program::ID && info.lamports() == 0 {
return Err(ErrorCode::AccountNotInitialized.into());
}
if info.owner != &T::owner() {
return Err(ErrorCode::AccountNotProgramOwned.into());
}
Expand Down
2 changes: 2 additions & 0 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub enum ErrorCode {
AccountNotSigner,
#[msg("The given account is not owned by the system program")]
AccountNotSystemOwned,
#[msg("The program expected this account to be already initialized")]
AccountNotInitialized,

// State.
#[msg("The given state account does not have the correct address")]
Expand Down
14 changes: 13 additions & 1 deletion tests/errors/programs/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ mod errors {
pub fn raw_custom_error(_ctx: Context<RawCustomError>) -> Result<()> {
Ok(())
}

pub fn account_not_initialized_error(_ctx: Context<AccountNotInitializedError>) -> Result<()> {
Ok(())
}
}

#[derive(Accounts)]
Expand Down Expand Up @@ -68,7 +72,15 @@ pub struct HasOneAccount {
#[derive(Accounts)]
pub struct RawCustomError<'info> {
#[account(constraint = *my_account.key == ID @ MyError::HelloCustom)]
my_account: AccountInfo<'info>
my_account: AccountInfo<'info>,
}

#[account]
pub struct AnyAccount {}

#[derive(Accounts)]
pub struct AccountNotInitializedError<'info> {
not_initialized_account: Account<'info, AnyAccount>,
}

#[error]
Expand Down
14 changes: 14 additions & 0 deletions tests/errors/tests/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,18 @@ describe("errors", () => {
assert.equal(err.code, 300 + 125);
}
});

it("Emits a account not initialized error", async () => {
try {
const tx = await program.rpc.accountNotInitializedError({
accounts: {
notInitializedAccount: (new anchor.web3.Keypair()).publicKey
},
});
assert.fail("Unexpected success in creating a transaction that should have fail with `AccountNotInitialized` error");
} catch (err) {
const errMsg = "The program expected this account to be already initialized";
assert.equal(err.toString(), errMsg);
}
});
});
5 changes: 5 additions & 0 deletions ts/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const LangErrorCode = {
InvalidProgramExecutable: 169,
AccountNotSigner: 170,
AccountNotSystemOwned: 171,
AccountNotInitialized: 172,

// State.
StateInvalidAddress: 180,
Expand Down Expand Up @@ -175,6 +176,10 @@ const LangErrorMessage = new Map([
LangErrorCode.AccountNotSystemOwned,
"The given account is not owned by the system program",
],
[
LangErrorCode.AccountNotInitialized,
"The program expected this account to be already initialized",
],

// State.
[
Expand Down