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

feat: handle MethodNotFound errors in JsonRpcProvider #1028

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion packages/near-api-js/fetch_error_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const https = require('https');
const fs = require('fs');

const ERROR_SCHEMA_URL =
'https://raw.githubusercontent.com/nearprotocol/nearcore/4c1149974ccf899dbcb2253a3e27cbab86dc47be/chain/jsonrpc/res/rpc_errors_schema.json';
'https://raw.githubusercontent.com/near/nearcore/master/chain/jsonrpc/res/rpc_errors_schema.json';
const TARGET_DIR = process.argv[2] || process.cwd() + '/src/generated';
const TARGET_SCHEMA_FILE_PATH = TARGET_DIR + '/rpc_error_schema.json';

Expand Down
4 changes: 3 additions & 1 deletion packages/near-api-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"compile": "tsc -p ./tsconfig.json",
"dev": "pnpm compile -w",
"build": "pnpm compile && pnpm browserify",
"test": "jest test",
"test": "concurrently \"pnpm:test:*\"",
"test:jest": "jest test",
"test:errorsjson": "node fetch_error_schema.js && git diff --exit-code",
"lint": "concurrently \"pnpm:lint:*(!fix)\"",
"lint:src": "eslint --ext .ts src",
"lint:test": "eslint --ext .js test",
Expand Down
149 changes: 71 additions & 78 deletions packages/near-api-js/src/generated/rpc_error_schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"schema": {
"AltBn128InvalidInput": {
"name": "AltBn128InvalidInput",
"subtypes": [],
"props": {
"msg": ""
}
},
"BadUTF16": {
"name": "BadUTF16",
"subtypes": [],
Expand All @@ -15,21 +22,6 @@
"subtypes": [],
"props": {}
},
"BreakpointTrap": {
"name": "BreakpointTrap",
"subtypes": [],
"props": {}
},
"CacheError": {
"name": "CacheError",
"subtypes": [
"ReadError",
"WriteError",
"DeserializationError",
"SerializationError"
],
"props": {}
},
"CallIndirectOOB": {
"name": "CallIndirectOOB",
"subtypes": [],
Expand Down Expand Up @@ -81,29 +73,25 @@
"subtypes": [],
"props": {}
},
"DeserializationError": {
"name": "DeserializationError",
"ECRecoverError": {
"name": "ECRecoverError",
"subtypes": [],
"props": {}
"props": {
"msg": ""
}
},
"Ed25519VerifyInvalidInput": {
"name": "Ed25519VerifyInvalidInput",
"subtypes": [],
"props": {
"msg": ""
}
},
"EmptyMethodName": {
"name": "EmptyMethodName",
"subtypes": [],
"props": {}
},
"FunctionCallError": {
"name": "FunctionCallError",
"subtypes": [
"CompilationError",
"LinkError",
"MethodResolveError",
"WasmTrap",
"WasmUnknownError",
"HostError",
"EvmError"
],
"props": {}
},
"GasExceeded": {
"name": "GasExceeded",
"subtypes": [],
Expand Down Expand Up @@ -163,7 +151,10 @@
"NumberInputDataDependenciesExceeded",
"ReturnedValueLengthExceeded",
"ContractSizeExceeded",
"Deprecated"
"Deprecated",
"ECRecoverError",
"AltBn128InvalidInput",
"Ed25519VerifyInvalidInput"
],
"props": {}
},
Expand All @@ -177,6 +168,11 @@
"subtypes": [],
"props": {}
},
"IndirectCallToNull": {
"name": "IndirectCallToNull",
"subtypes": [],
"props": {}
},
"Instantiate": {
"name": "Instantiate",
"subtypes": [],
Expand Down Expand Up @@ -259,13 +255,6 @@
"limit": ""
}
},
"LinkError": {
"name": "LinkError",
"subtypes": [],
"props": {
"msg": ""
}
},
"Memory": {
"name": "Memory",
"subtypes": [],
Expand Down Expand Up @@ -300,17 +289,11 @@
"name": "MethodResolveError",
"subtypes": [
"MethodEmptyName",
"MethodUTF8Error",
"MethodNotFound",
"MethodInvalidSignature"
],
"props": {}
},
"MethodUTF8Error": {
"name": "MethodUTF8Error",
"subtypes": [],
"props": {}
},
"MisalignedAtomicAccess": {
"name": "MisalignedAtomicAccess",
"subtypes": [],
Expand Down Expand Up @@ -348,7 +331,9 @@
"GasInstrumentation",
"StackHeightInstrumentation",
"Instantiate",
"Memory"
"Memory",
"TooManyFunctions",
"TooManyLocals"
],
"props": {}
},
Expand All @@ -359,11 +344,6 @@
"method_name": ""
}
},
"ReadError": {
"name": "ReadError",
"subtypes": [],
"props": {}
},
"ReturnedValueLengthExceeded": {
"name": "ReturnedValueLengthExceeded",
"subtypes": [],
Expand All @@ -377,13 +357,6 @@
"subtypes": [],
"props": {}
},
"SerializationError": {
"name": "SerializationError",
"subtypes": [],
"props": {
"hash": ""
}
},
"StackHeightInstrumentation": {
"name": "StackHeightInstrumentation",
"subtypes": [],
Expand All @@ -394,6 +367,16 @@
"subtypes": [],
"props": {}
},
"TooManyFunctions": {
"name": "TooManyFunctions",
"subtypes": [],
"props": {}
},
"TooManyLocals": {
"name": "TooManyLocals",
"subtypes": [],
"props": {}
},
"TotalLogLengthExceeded": {
"name": "TotalLogLengthExceeded",
"subtypes": [],
Expand Down Expand Up @@ -424,29 +407,19 @@
"CallIndirectOOB",
"IllegalArithmetic",
"MisalignedAtomicAccess",
"BreakpointTrap",
"IndirectCallToNull",
"StackOverflow",
"GenericTrap"
],
"props": {}
},
"WasmUnknownError": {
"name": "WasmUnknownError",
"subtypes": [],
"props": {}
},
"WasmerCompileError": {
"name": "WasmerCompileError",
"subtypes": [],
"props": {
"msg": ""
}
},
"WriteError": {
"name": "WriteError",
"subtypes": [],
"props": {}
},
"AccessKeyNotFound": {
"name": "AccessKeyNotFound",
"subtypes": [],
Expand Down Expand Up @@ -486,7 +459,8 @@
"InsufficientStake",
"FunctionCallError",
"NewReceiptValidationError",
"OnlyImplicitAccountCreationAllowed"
"OnlyImplicitAccountCreationAllowed",
"DeleteAccountWithLargeState"
],
"props": {
"index": ""
Expand Down Expand Up @@ -589,6 +563,13 @@
"account_id": ""
}
},
"DeleteAccountWithLargeState": {
"name": "DeleteAccountWithLargeState",
"subtypes": [],
"props": {
"account_id": ""
}
},
"DeleteActionMustBeFinal": {
"name": "DeleteActionMustBeFinal",
"subtypes": [],
Expand Down Expand Up @@ -707,14 +688,16 @@
"InvalidSignerId",
"SignerDoesNotExist",
"InvalidNonce",
"NonceTooLarge",
"InvalidReceiverId",
"InvalidSignature",
"NotEnoughBalance",
"LackBalanceForState",
"CostOverflow",
"InvalidChain",
"Expired",
"ActionsValidation"
"ActionsValidation",
"TransactionSizeExceeded"
],
"props": {}
},
Expand All @@ -733,6 +716,14 @@
"method_name": ""
}
},
"NonceTooLarge": {
"name": "NonceTooLarge",
"subtypes": [],
"props": {
"tx_nonce": "",
"upper_bound": ""
}
},
"NotEnoughAllowance": {
"name": "NotEnoughAllowance",
"subtypes": [],
Expand Down Expand Up @@ -808,6 +799,14 @@
"total_prepaid_gas": ""
}
},
"TransactionSizeExceeded": {
"name": "TransactionSizeExceeded",
"subtypes": [],
"props": {
"limit": "",
"size": ""
}
},
"TriesToStake": {
"name": "TriesToStake",
"subtypes": [],
Expand Down Expand Up @@ -845,18 +844,12 @@
"subtypes": [],
"props": {}
},
"InternalError": {
"name": "InternalError",
"subtypes": [],
"props": {}
},
"ServerError": {
"name": "ServerError",
"subtypes": [
"TxExecutionError",
"Timeout",
"Closed",
"InternalError"
"Closed"
],
"props": {}
},
Expand Down
10 changes: 8 additions & 2 deletions packages/near-api-js/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { ConnectionInfo, fetchJson } from '../utils/web';
import { TypedError, ErrorContext } from '../utils/errors';
import { baseEncode } from 'borsh';
import exponentialBackoff from '../utils/exponential-backoff';
import { parseRpcError, getErrorTypeFromErrorMessage } from '../utils/rpc_errors';
import { parseRpcError, getErrorTypeFromErrorMessage, ServerError, formatError } from '../utils/rpc_errors';
import { SignedTransaction } from '../transaction';

/** @hidden */
Expand Down Expand Up @@ -347,7 +347,13 @@ export class JsonRpcProvider extends Provider {
throw new TypedError(errorMessage, 'TimeoutError');
}

throw new TypedError(errorMessage, getErrorTypeFromErrorMessage(response.error.data, response.error.name));
const errorType = getErrorTypeFromErrorMessage(response.error.data, response.error.name);
throw new TypedError(formatError(errorType, params) || errorMessage, errorType);
}
} else if (typeof response.result?.error === 'string') {
const errorType = getErrorTypeFromErrorMessage(response.result.error, '');
if (errorType) {
throw new ServerError(formatError(errorType, { method }), errorType);
}
}
// Success when response.error is not exist
Expand Down
2 changes: 2 additions & 0 deletions packages/near-api-js/src/utils/rpc_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export function getErrorTypeFromErrorMessage(errorMessage, errorType) {
return 'AccessKeyDoesNotExist';
case /wasm execution failed with error: FunctionCallError\(CompilationError\(CodeDoesNotExist/.test(errorMessage):
return 'CodeDoesNotExist';
case /wasm execution failed with error: FunctionCallError\(MethodResolveError\(MethodNotFound/.test(errorMessage):
return 'MethodNotFound';
case /Transaction nonce \d+ must be larger than nonce of the used access key \d+/.test(errorMessage):
return 'InvalidNonce';
default:
Expand Down
Loading