-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create coded errors to cover all
TransactionErrors
from the RPC (#2213
) # Summary Wouldn't it be nice if you could `catch` _particular_ transaction errors in your application, like `BlockhashNotFound`, and choose the correct mitigation based on the type of transaction error? In this PR, we introduce coded exceptions for each `TransactionError` returned from the RPC's `sendTransaction` method. > [!NOTE] > Because the RPC doesn't return structured errors or error codes, we had to break our own rules in this PR and hardcode a map between the error names and the code numbers. My [first crack](https://gist.github.com/steveluscher/aaa7cbbb5433b1197983908a40860c47#file-fml-ts-L12) at this employed a source code compression scheme that I later deemed too risky for the 250 gzipped bytes it saved. We might consider such a scheme in the future, especially since the next PR will add `InstructionError` to the mix. # Test Plan ```shell cd packages/errors pnpm test:unit:browser pnpm test:unit:node ``` Addresses #2118.
- Loading branch information
1 parent
65af153
commit 8541c2e
Showing
8 changed files
with
397 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,94 @@ | ||
import { | ||
SOLANA_ERROR__TRANSACTION_ERROR_DUPLICATE_INSTRUCTION, | ||
SOLANA_ERROR__TRANSACTION_ERROR_INSUFFICIENT_FUNDS_FOR_RENT, | ||
SOLANA_ERROR__TRANSACTION_ERROR_PROGRAM_EXECUTION_TEMPORARILY_RESTRICTED, | ||
SOLANA_ERROR__TRANSACTION_ERROR_UNKNOWN, | ||
SolanaErrorCode, | ||
} from '../codes'; | ||
import { SolanaError } from '../error'; | ||
import { getSolanaErrorFromTransactionError } from '../transaction-error'; | ||
|
||
describe('getSolanaErrorFromTransactionError', () => { | ||
it.each([ | ||
['AccountInUse', 7050001], | ||
['AccountLoadedTwice', 7050002], | ||
['AccountNotFound', 7050003], | ||
['ProgramAccountNotFound', 7050004], | ||
['InsufficientFundsForFee', 7050005], | ||
['InvalidAccountForFee', 7050006], | ||
['AlreadyProcessed', 7050007], | ||
['BlockhashNotFound', 7050008], | ||
['CallChainTooDeep', 7050009], | ||
['MissingSignatureForFee', 7050010], | ||
['InvalidAccountIndex', 7050011], | ||
['SignatureFailure', 7050012], | ||
['InvalidProgramForExecution', 7050013], | ||
['SanitizeFailure', 7050014], | ||
['ClusterMaintenance', 7050015], | ||
['AccountBorrowOutstanding', 7050016], | ||
['WouldExceedMaxBlockCostLimit', 7050017], | ||
['UnsupportedVersion', 7050018], | ||
['InvalidWritableAccount', 7050019], | ||
['WouldExceedMaxAccountCostLimit', 7050020], | ||
['WouldExceedAccountDataBlockLimit', 7050021], | ||
['TooManyAccountLocks', 7050022], | ||
['AddressLookupTableNotFound', 7050023], | ||
['InvalidAddressLookupTableOwner', 7050024], | ||
['InvalidAddressLookupTableData', 7050025], | ||
['InvalidAddressLookupTableIndex', 7050026], | ||
['InvalidRentPayingAccount', 7050027], | ||
['WouldExceedMaxVoteCostLimit', 7050028], | ||
['WouldExceedAccountDataTotalLimit', 7050029], | ||
['MaxLoadedAccountsDataSizeExceeded', 7050032], | ||
['InvalidLoadedAccountsDataSizeLimit', 7050033], | ||
['ResanitizationNeeded', 7050034], | ||
['UnbalancedTransaction', 7050036], | ||
])('produces the correct `SolanaError` for a `%s` error', (transactionError, expectedCode) => { | ||
const error = getSolanaErrorFromTransactionError(transactionError); | ||
expect(error).toEqual(new SolanaError(expectedCode as SolanaErrorCode, undefined)); | ||
}); | ||
it('produces the correct `SolanaError` for a `DuplicateInstruction` error', () => { | ||
const error = getSolanaErrorFromTransactionError({ DuplicateInstruction: 1 }); | ||
expect(error).toEqual( | ||
new SolanaError(SOLANA_ERROR__TRANSACTION_ERROR_DUPLICATE_INSTRUCTION, { | ||
index: 1, | ||
}), | ||
); | ||
}); | ||
it('produces the correct `SolanaError` for a `InsufficientFundsForRent` error', () => { | ||
const error = getSolanaErrorFromTransactionError({ InsufficientFundsForRent: { account_index: 1 } }); | ||
expect(error).toEqual( | ||
new SolanaError(SOLANA_ERROR__TRANSACTION_ERROR_INSUFFICIENT_FUNDS_FOR_RENT, { | ||
accountIndex: 1, | ||
}), | ||
); | ||
}); | ||
it('produces the correct `SolanaError` for a `ProgramExecutionTemporarilyRestricted` error', () => { | ||
const error = getSolanaErrorFromTransactionError({ | ||
ProgramExecutionTemporarilyRestricted: { account_index: 1 }, | ||
}); | ||
expect(error).toEqual( | ||
new SolanaError(SOLANA_ERROR__TRANSACTION_ERROR_PROGRAM_EXECUTION_TEMPORARILY_RESTRICTED, { | ||
accountIndex: 1, | ||
}), | ||
); | ||
}); | ||
it("returns the unknown error when encountering an enum name that's missing from the map", () => { | ||
const error = getSolanaErrorFromTransactionError('ThisDoesNotExist'); | ||
expect(error).toEqual( | ||
new SolanaError(SOLANA_ERROR__TRANSACTION_ERROR_UNKNOWN, { | ||
errorName: 'ThisDoesNotExist', | ||
}), | ||
); | ||
}); | ||
it("returns the unknown error when encountering an enum struct that's missing from the map", () => { | ||
const expectedContext = {} as const; | ||
const error = getSolanaErrorFromTransactionError({ ThisDoesNotExist: expectedContext }); | ||
expect(error).toEqual( | ||
new SolanaError(SOLANA_ERROR__TRANSACTION_ERROR_UNKNOWN, { | ||
errorName: 'ThisDoesNotExist', | ||
transactionErrorContext: expectedContext, | ||
}), | ||
); | ||
}); | ||
}); |
This file contains 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 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 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,2 +1,3 @@ | ||
export * from './codes'; | ||
export * from './error'; | ||
export * from './transaction-error'; |
Oops, something went wrong.