-
Notifications
You must be signed in to change notification settings - Fork 34
program: Add custom errors #51
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * This code was AUTOGENERATED using the Codama library. | ||
| * Please DO NOT EDIT THIS FILE, instead use visitors | ||
| * to add features, then rerun Codama to update it. | ||
| * | ||
| * @see https://github.com/codama-idl/codama | ||
| */ | ||
|
|
||
| export * from './programMetadata'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * This code was AUTOGENERATED using the Codama library. | ||
| * Please DO NOT EDIT THIS FILE, instead use visitors | ||
| * to add features, then rerun Codama to update it. | ||
| * | ||
| * @see https://github.com/codama-idl/codama | ||
| */ | ||
|
|
||
| import { | ||
| isProgramError, | ||
| type Address, | ||
| type SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM, | ||
| type SolanaError, | ||
| } from '@solana/kit'; | ||
| import { PROGRAM_METADATA_PROGRAM_ADDRESS } from '../programs'; | ||
|
|
||
| /** NotExecutableAccount: The program account is not executable */ | ||
| export const PROGRAM_METADATA_ERROR__NOT_EXECUTABLE_ACCOUNT = 0x0; // 0 | ||
| /** InvalidProgramState: The program state is invalid */ | ||
| export const PROGRAM_METADATA_ERROR__INVALID_PROGRAM_STATE = 0x1; // 1 | ||
| /** InvalidProgramDataAccount: The program data account is invalid */ | ||
| export const PROGRAM_METADATA_ERROR__INVALID_PROGRAM_DATA_ACCOUNT = 0x2; // 2 | ||
| /** ImmutableMetadataAccount: The metadata account is immutable */ | ||
| export const PROGRAM_METADATA_ERROR__IMMUTABLE_METADATA_ACCOUNT = 0x3; // 3 | ||
| /** InvalidDataLength: The account data length is invalid */ | ||
| export const PROGRAM_METADATA_ERROR__INVALID_DATA_LENGTH = 0x4; // 4 | ||
|
|
||
| export type ProgramMetadataError = | ||
| | typeof PROGRAM_METADATA_ERROR__IMMUTABLE_METADATA_ACCOUNT | ||
| | typeof PROGRAM_METADATA_ERROR__INVALID_DATA_LENGTH | ||
| | typeof PROGRAM_METADATA_ERROR__INVALID_PROGRAM_DATA_ACCOUNT | ||
| | typeof PROGRAM_METADATA_ERROR__INVALID_PROGRAM_STATE | ||
| | typeof PROGRAM_METADATA_ERROR__NOT_EXECUTABLE_ACCOUNT; | ||
|
|
||
| let programMetadataErrorMessages: Record<ProgramMetadataError, string> | undefined; | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| programMetadataErrorMessages = { | ||
| [PROGRAM_METADATA_ERROR__IMMUTABLE_METADATA_ACCOUNT]: `The metadata account is immutable`, | ||
| [PROGRAM_METADATA_ERROR__INVALID_DATA_LENGTH]: `The account data length is invalid`, | ||
| [PROGRAM_METADATA_ERROR__INVALID_PROGRAM_DATA_ACCOUNT]: `The program data account is invalid`, | ||
| [PROGRAM_METADATA_ERROR__INVALID_PROGRAM_STATE]: `The program state is invalid`, | ||
| [PROGRAM_METADATA_ERROR__NOT_EXECUTABLE_ACCOUNT]: `The program account is not executable`, | ||
| }; | ||
| } | ||
|
|
||
| export function getProgramMetadataErrorMessage(code: ProgramMetadataError): string { | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| return (programMetadataErrorMessages as Record<ProgramMetadataError, string>)[code]; | ||
| } | ||
|
|
||
| return 'Error message not available in production bundles.'; | ||
| } | ||
|
|
||
| export function isProgramMetadataError<TProgramErrorCode extends ProgramMetadataError>( | ||
| error: unknown, | ||
| transactionMessage: { instructions: Record<number, { programAddress: Address }> }, | ||
| code?: TProgramErrorCode, | ||
| ): error is SolanaError<typeof SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM> & | ||
| Readonly<{ context: Readonly<{ code: TProgramErrorCode }> }> { | ||
| return isProgramError<TProgramErrorCode>(error, transactionMessage, PROGRAM_METADATA_PROGRAM_ADDRESS, code); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //! This code was AUTOGENERATED using the codama library. | ||
| //! Please DO NOT EDIT THIS FILE, instead use visitors | ||
| //! to add features, then rerun codama to update it. | ||
| //! | ||
| //! <https://github.com/codama-idl/codama> | ||
| //! | ||
|
|
||
| use num_derive::FromPrimitive; | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)] | ||
| pub enum ProgramMetadataError { | ||
| /// 0 - The program account is not executable | ||
| #[error("The program account is not executable")] | ||
| NotExecutableAccount = 0x0, | ||
| /// 1 - The program state is invalid | ||
| #[error("The program state is invalid")] | ||
| InvalidProgramState = 0x1, | ||
| /// 2 - The program data account is invalid | ||
| #[error("The program data account is invalid")] | ||
| InvalidProgramDataAccount = 0x2, | ||
| /// 3 - The metadata account is immutable | ||
| #[error("The metadata account is immutable")] | ||
| ImmutableMetadataAccount = 0x3, | ||
| /// 4 - The account data length is invalid | ||
| #[error("The account data length is invalid")] | ||
| InvalidDataLength = 0x4, | ||
| } | ||
|
|
||
| impl From<ProgramMetadataError> for solana_program_error::ProgramError { | ||
| fn from(e: ProgramMetadataError) -> Self { | ||
| solana_program_error::ProgramError::Custom(e as u32) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lorisleiva Not sure if this is the best way to assert a custom error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perfect!