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
7 changes: 6 additions & 1 deletion 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 @@ -349,6 +349,11 @@ export class JsonRpcProvider extends Provider {

throw new TypedError(errorMessage, getErrorTypeFromErrorMessage(response.error.data, response.error.name));
}
} 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
return response;
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
70 changes: 70 additions & 0 deletions packages/near-api-js/test/providers_errors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const nearApi = require('../src/index');
const testUtils = require('./test-utils');
let ERRORS_JSON = require('../src/res/error_messages.json');

jest.setTimeout(20000);

const withProvider = (fn) => {
const config = Object.assign(require('./config')(process.env.NODE_ENV || 'test'));
const provider = new nearApi.providers.JsonRpcProvider(config.nodeUrl);
return () => fn(provider);
};

test('JSON RPC Error: MethodNotFound', withProvider(async (provider) => {
const near = await testUtils.setUpTestConnection();
const account = await testUtils.createAccount(near);
const contract = await testUtils.deployContract(account, testUtils.generateUniqueString('test'));

await contract.setValue({ args: { value: 'hello' } });

try {
const response = await provider.query({
request_type: 'call_function',
finality: 'final',
account_id: contract.contractId,
method_name: 'methodNameThatDoesNotExist',
args_base64: ''
});
expect(response).toBeUndefined();
} catch (e) {
const errorType = 'MethodNotFound';
expect(e.type).toEqual(errorType);
expect(e.message).toEqual(ERRORS_JSON[errorType]);
}

}));

test('JSON RPC Error: CodeDoesNotExist', withProvider(async (provider) => {
try {
const response = await provider.query({
request_type: 'call_function',
finality: 'final',
account_id: 'test.near',
method_name: 'methodNameThatDoesNotExistOnContractNotDeployed',
args_base64: ''
});
expect(response).toBeUndefined();
} catch (e) {
const errorType = 'CodeDoesNotExist';
expect(e.type).toEqual(errorType);
expect(e.message.split(' ').slice(0, 5)).toEqual(ERRORS_JSON[errorType].split(' ').slice(0, 5));
}
}));

test('JSON RPC Error: AccountDoesNotExist', withProvider(async (provider) => {
const accountName = 'abc.near';
try {
const response = await provider.query({
request_type: 'call_function',
finality: 'final',
account_id: accountName,
method_name: 'methodNameThatDoesNotExistOnContractNotDeployed',
args_base64: ''
});
expect(response).toBeUndefined();
} catch (e) {
const errorType = 'AccountDoesNotExist';
expect(e.type).toEqual(errorType);
expect(e.message).toEqual(`[-32000] Server error: account ${accountName} does not exist while viewing`);
idea404 marked this conversation as resolved.
Show resolved Hide resolved
}
}));