-
Notifications
You must be signed in to change notification settings - Fork 6
fix(webapp): map custom program errors per failing program #158
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
+33
−7
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,35 +1,61 @@ | ||
| import idl from '@idl'; | ||
| import { TOKEN_PROGRAM_ADDRESS } from '@solana-program/token'; | ||
| import { TOKEN_2022_PROGRAM_ADDRESS } from '@solana-program/token-2022'; | ||
|
|
||
| type IdlError = { code: number; name: string; message: string }; | ||
|
|
||
| const errors = (idl.program?.errors ?? []) as IdlError[]; | ||
| const PROGRAM_ERRORS: Record<number, string> = Object.fromEntries(errors.map(e => [e.code, e.message])); | ||
| const PROGRAM_ADDRESS = idl.program?.publicKey ?? ''; | ||
|
|
||
| const SPL_TOKEN_ERRORS: Record<number, string> = { | ||
| 0: 'Account not rent exempt', | ||
| 1: 'Insufficient funds', | ||
| 2: 'Invalid mint', | ||
| 3: 'Mint mismatch', | ||
| 3: 'Account not associated with this mint', | ||
| 4: 'Owner mismatch', | ||
| 5: 'Mint has a fixed supply', | ||
| 6: 'Account already in use', | ||
| 7: 'Invalid number of provided signers', | ||
| 8: 'Invalid number of required signers', | ||
| 9: 'Token account is not initialized', | ||
| 10: 'Instruction does not support native tokens', | ||
| 11: 'Non-native account can only be closed if its balance is zero', | ||
| 12: 'Invalid instruction', | ||
| 13: 'Account is in an invalid state for this operation', | ||
| 14: 'Operation overflowed', | ||
| 15: 'Account does not support the specified authority type', | ||
| 16: 'This mint cannot freeze accounts', | ||
| 17: 'Token account is frozen', | ||
| 18: 'Decimals do not match the mint', | ||
| 19: 'Instruction does not support non-native tokens', | ||
| }; | ||
|
|
||
| export function parseProgramError(error: unknown, programAddress?: string): string { | ||
| const TOKEN_PROGRAM_IDS = new Set<string>([TOKEN_PROGRAM_ADDRESS, TOKEN_2022_PROGRAM_ADDRESS]); | ||
|
|
||
| export function parseProgramError(error: unknown): string { | ||
| if (!(error instanceof Error)) return 'Unknown error'; | ||
|
|
||
| const hexMatch = error.message.match(/custom program error: 0x([0-9a-fA-F]+)/i); | ||
| const decMatch = error.message.match(/Custom\((\d+)\)/); | ||
|
|
||
| const code = hexMatch ? parseInt(hexMatch[1], 16) : decMatch ? parseInt(decMatch[1], 10) : null; | ||
|
|
||
| if (code === null) return error.message; | ||
|
|
||
| const failLineMatch = error.message.match(/Program (\w+) failed: custom program error:/); | ||
| const failedProgram = failLineMatch?.[1] ?? ''; | ||
|
|
||
| if (!programAddress || failedProgram === programAddress) { | ||
| const msg = PROGRAM_ERRORS[code]; | ||
| if (msg) return msg; | ||
| if (failedProgram === PROGRAM_ADDRESS) { | ||
| return PROGRAM_ERRORS[code] ?? `Subscriptions program error ${code}`; | ||
| } | ||
|
|
||
| if (TOKEN_PROGRAM_IDS.has(failedProgram)) { | ||
| return SPL_TOKEN_ERRORS[code] ?? `Token program error ${code}`; | ||
| } | ||
|
|
||
| if (failedProgram) { | ||
| return `Program ${failedProgram} error ${code}`; | ||
| } | ||
|
|
||
| return SPL_TOKEN_ERRORS[code] ?? `Program error ${code}`; | ||
| return PROGRAM_ERRORS[code] ?? `Program error ${code}`; | ||
| } | ||
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.