-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
RpcError
with a coded exception
- Loading branch information
1 parent
f886f55
commit 588e110
Showing
16 changed files
with
598 additions
and
297 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,104 @@ | ||
import { | ||
SOLANA_ERROR__JSON_RPC__INTERNAL_ERROR, | ||
SOLANA_ERROR__JSON_RPC__INVALID_PARAMS, | ||
SOLANA_ERROR__JSON_RPC__INVALID_REQUEST, | ||
SOLANA_ERROR__JSON_RPC__METHOD_NOT_FOUND, | ||
SOLANA_ERROR__JSON_RPC__PARSE_ERROR, | ||
SOLANA_ERROR__JSON_RPC__SCAN_ERROR, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_CLEANED_UP, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_NOT_AVAILABLE, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_NO_SNAPSHOT, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_NODE_UNHEALTHY, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SLOT_SKIPPED, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_PRECOMPILE_VERIFICATION_FAILURE, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_SIGNATURE_LEN_MISMATCH, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_SIGNATURE_VERIFICATION_FAILURE, | ||
SOLANA_ERROR__JSON_RPC__SERVER_ERROR_UNSUPPORTED_TRANSACTION_VERSION, | ||
SolanaErrorCode, | ||
} from '../codes'; | ||
import { SolanaErrorContext } from '../context'; | ||
import { SolanaError } from '../error'; | ||
import { getSolanaErrorFromJsonRpcError } from '../json-rpc-error'; | ||
import { getSolanaErrorFromTransactionError } from '../transaction-error'; | ||
|
||
jest.mock('../transaction-error.ts'); | ||
|
||
describe('getSolanaErrorFromJsonRpcError', () => { | ||
it.each([ | ||
[SOLANA_ERROR__JSON_RPC__PARSE_ERROR, -32700], | ||
[SOLANA_ERROR__JSON_RPC__INTERNAL_ERROR, -32603], | ||
[SOLANA_ERROR__JSON_RPC__INVALID_PARAMS, -32602], | ||
[SOLANA_ERROR__JSON_RPC__METHOD_NOT_FOUND, -32601], | ||
[SOLANA_ERROR__JSON_RPC__INVALID_REQUEST, -32600], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED, -32016], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_UNSUPPORTED_TRANSACTION_VERSION, -32015], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET, -32014], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_SIGNATURE_LEN_MISMATCH, -32013], | ||
[SOLANA_ERROR__JSON_RPC__SCAN_ERROR, -32012], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE, -32011], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX, -32010], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED, -32009], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_NO_SNAPSHOT, -32008], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SLOT_SKIPPED, -32007], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_PRECOMPILE_VERIFICATION_FAILURE, -32006], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_NODE_UNHEALTHY, -32005], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_NOT_AVAILABLE, -32004], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_TRANSACTION_SIGNATURE_VERIFICATION_FAILURE, -32003], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE, -32002], | ||
[SOLANA_ERROR__JSON_RPC__SERVER_ERROR_BLOCK_CLEANED_UP, -32001], | ||
])( | ||
'produces the `SolanaError` code `%s` given a %s JSON-RPC 2.0 error code', | ||
(expectedSolanaErrorCode, jsonRpcErrorCode) => { | ||
const error = getSolanaErrorFromJsonRpcError({ code: jsonRpcErrorCode, message: 'o no' }); | ||
expect(error).toHaveProperty('context.__code', expectedSolanaErrorCode); | ||
}, | ||
); | ||
it('produces a `SolanaError` given a JSON-RPC error with data', () => { | ||
const expectedData = Symbol() as unknown as SolanaErrorContext[SolanaErrorCode]; | ||
const error = getSolanaErrorFromJsonRpcError({ code: 0, data: expectedData, message: 'o no' }); | ||
expect(error).toEqual(new SolanaError(7766768 as SolanaErrorCode, expectedData)); | ||
}); | ||
it('produces a `SolanaError` with the message as the data given a JSON-RPC error with no data', () => { | ||
const error = getSolanaErrorFromJsonRpcError({ code: 0, data: null, message: 'o no' }); | ||
expect(error).toEqual(new SolanaError(7766768 as SolanaErrorCode, { message: 'o no' })); | ||
}); | ||
describe('when passed a preflight failure', () => { | ||
it('produces a `SolanaError` with the transaction error as the `cause`', () => { | ||
const mockErrorResult = Symbol() as unknown as SolanaError; | ||
jest.mocked(getSolanaErrorFromTransactionError).mockReturnValue(mockErrorResult); | ||
const error = getSolanaErrorFromJsonRpcError({ | ||
code: -32002 /* Corresponds to SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE */, | ||
data: { err: Symbol() }, | ||
message: 'o no', | ||
}); | ||
expect(error.cause).toBe(mockErrorResult); | ||
}); | ||
it('produces a `SolanaError` with the preflight failure data (minus the `err` property) as the context', () => { | ||
const preflightErrorData = { bar: 2, baz: 3, foo: 1 }; | ||
const error = getSolanaErrorFromJsonRpcError({ | ||
code: -32002 /* Corresponds to SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE */, | ||
data: { ...preflightErrorData, err: Symbol() }, | ||
message: 'o no', | ||
}); | ||
expect(error.context).toEqual({ | ||
__code: SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE, | ||
...preflightErrorData, | ||
}); | ||
}); | ||
it('delegates `err` to the transaction error getter', () => { | ||
const transactionError = Symbol(); | ||
getSolanaErrorFromJsonRpcError({ | ||
code: -32002 /* Corresponds to SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE */, | ||
data: { err: transactionError }, | ||
message: 'o no', | ||
}); | ||
expect(getSolanaErrorFromTransactionError).toHaveBeenCalledWith(transactionError); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.