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

Implement TryFromIntError conversion for AnchorError #2950

Merged
merged 8 commits into from
May 15, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- avm: Support customizing the installation location using `AVM_HOME` environment variable ([#2917](https://github.com/coral-xyz/anchor/pull/2917)).
- idl, ts: Add accounts resolution for associated token accounts ([#2927](https://github.com/coral-xyz/anchor/pull/2927)).
- cli: Add `--no-install` option to the `init` command ([#2945](https://github.com/coral-xyz/anchor/pull/2945)).
- lang: Implement `TryFromIntError` for `Error` to be able to propagate integer conversion errors ([#2950](https://github.com/coral-xyz/anchor/pull/2950)).

### Fixes

Expand Down
16 changes: 16 additions & 0 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anchor_lang::error_code;
use borsh::maybestd::io::Error as BorshIoError;
use solana_program::{program_error::ProgramError, pubkey::Pubkey};
use std::fmt::{Debug, Display};
use std::num::TryFromIntError;

/// The starting point for user defined error codes.
pub const ERROR_CODE_OFFSET: u32 = 6000;
Expand Down Expand Up @@ -263,6 +264,9 @@ pub enum ErrorCode {
/// 4101 - You cannot/should not initialize the payer account as a program account
#[msg("You cannot/should not initialize the payer account as a program account")]
TryingToInitPayerAsProgramAccount = 4101,
/// 4102 - Invalid numeric conversion error
#[msg("Error during numeric conversion")]
InvalidNumericConversion = 4102,

// Deprecated
/// 5000 - The API being used is deprecated and should no longer be used
Expand Down Expand Up @@ -310,6 +314,18 @@ impl From<ProgramErrorWithOrigin> for Error {
}
}

impl From<TryFromIntError> for Error {
fn from(e: TryFromIntError) -> Self {
Self::AnchorError(Box::new(AnchorError {
error_name: ErrorCode::InvalidNumericConversion.name(),
error_code_number: ErrorCode::InvalidNumericConversion.into(),
error_msg: format!("{}", e),
error_origin: None,
compared_values: None,
}))
}
}

impl Error {
pub fn log(&self) {
match self {
Expand Down
4 changes: 4 additions & 0 deletions tests/errors/programs/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ mod errors {
require_gte!(5, 10);
Ok(())
}

pub fn try_into_integer(_ctx: Context<Hello>) -> Result<i64> {
Ok(u64::MAX.try_into()?)
}
}

#[derive(Accounts)]
Expand Down
17 changes: 17 additions & 0 deletions tests/errors/tests/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,21 @@ describe("errors", () => {
"Program log: Right: 10",
]);
});

it("Emits a InvalidNumericConversion error via try_into", async () => {
await withLogTest(async () => {
try {
const tx = await program.methods.tryIntoInteger().rpc();
assert.fail(
"Unexpected success in creating a transaction that should have failed with `InvalidNumericConversion` error"
);
} catch (_err) {
assert.isTrue(_err instanceof AnchorError);
const err: AnchorError = _err;
assert.strictEqual(err.error.errorCode.number, 4102);
}
}, [
"Program log: AnchorError occurred. Error Code: InvalidNumericConversion. Error Number: 4102. Error Message: out of range integral type conversion attempted.",
]);
});
});
5 changes: 5 additions & 0 deletions ts/packages/anchor/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ export const LangErrorCode = {
// Miscellaneous
DeclaredProgramIdMismatch: 4100,
TryingToInitPayerAsProgramAccount: 4101,
InvalidNumericConversion: 4102,

// Used for APIs that shouldn't be used anymore.
Deprecated: 5000,
Expand Down Expand Up @@ -638,6 +639,10 @@ export const LangErrorMessage = new Map([
LangErrorCode.TryingToInitPayerAsProgramAccount,
"You cannot/should not initialize the payer account as a program account",
],
[
LangErrorCode.InvalidNumericConversion,
"The program could not perform the numeric conversion, out of range integral type conversion attempted",
],

// Deprecated
[
Expand Down
Loading