Skip to content

Commit

Permalink
lang: Add AccountNotInitialized error
Browse files Browse the repository at this point in the history
  • Loading branch information
cyphersnake committed Nov 16, 2021
1 parent 94de51b commit a2ddc62
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
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)).

### 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

0 comments on commit a2ddc62

Please sign in to comment.