Skip to content

Commit

Permalink
fix: removed name field in JsonRpcError class (hashgraph#2590)
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>

Revert "fix: removed name field in JsonRpcError class"

This reverts commit c8024e3.
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>

Reapply "fix: removed name field in JsonRpcError class"

This reverts commit e64d1fc.
  • Loading branch information
quiet-node authored Jun 13, 2024
1 parent e1ab6bf commit ef4c91e
Show file tree
Hide file tree
Showing 18 changed files with 6 additions and 96 deletions.
1 change: 0 additions & 1 deletion charts/hedera-json-rpc-relay/postman.json
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,6 @@
"pm.test(\"Success\", () => {",
" var response = pm.response.json();",
" pm.expect(response.error.code).to.equal(-32601);",
" pm.expect(response.error.name).to.equal(\"Method not found\");",
" pm.expect(response.error.message.endsWith(\"Unsupported JSON-RPC method\")).to.be.true;",
" pm.expect(response.id).to.equal(\"test_id\");",
" pm.expect(response.jsonrpc).to.equal(\"2.0\");",
Expand Down
48 changes: 1 addition & 47 deletions packages/relay/src/lib/errors/JsonRpcError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ import constants from '../../lib/constants';
export class JsonRpcError {
public code: number;
public message: string;
public name?: string;
public data?: string;

constructor(args: { name?: string; code: number; message: string; data?: string }, requestId?: string) {
constructor(args: { code: number; message: string; data?: string }, requestId?: string) {
this.code = args.code;
this.name = args.name;
this.message = requestId ? `[${constants.REQUEST_ID_STRING}${requestId}] ` + args.message : args.message;
this.data = args.data;
}
Expand All @@ -43,139 +41,114 @@ export const predefined = {
}),
GAS_LIMIT_TOO_HIGH: (gasLimit, maxGas) =>
new JsonRpcError({
name: 'gasLimit too high',
code: -32005,
message: `Transaction gas limit '${gasLimit}' exceeds block gas limit '${maxGas}'`,
}),
GAS_LIMIT_TOO_LOW: (gasLimit, requiredGas) =>
new JsonRpcError({
name: 'gasLimit too low',
code: -32003,
message: `Transaction gas limit provided '${gasLimit}' is insufficient of intrinsic gas required '${requiredGas}'`,
}),
GAS_PRICE_TOO_LOW: (gasPrice, minGasPrice) =>
new JsonRpcError({
name: 'Gas price too low',
code: -32009,
message: `Gas price '${gasPrice}' is below configured minimum gas price '${minGasPrice}'`,
}),
HBAR_RATE_LIMIT_EXCEEDED: new JsonRpcError({
name: 'HBAR Rate limit exceeded',
code: -32606,
message: 'HBAR Rate limit exceeded',
}),
INSUFFICIENT_ACCOUNT_BALANCE: new JsonRpcError({
name: 'Insufficient account balance',
code: -32000,
message: 'Insufficient funds for transfer',
}),
INTERNAL_ERROR: (message = '') =>
new JsonRpcError({
name: 'Internal error',
code: -32603,
message: message === '' || undefined ? 'Unknown error invoking RPC' : `Error invoking RPC: ${message}`,
}),
INVALID_PARAMETER: (index: number | string, message: string) =>
new JsonRpcError({
name: 'Invalid parameter',
code: -32602,
message: `Invalid parameter ${index}: ${message}`,
}),
INVALID_PARAMETERS: new JsonRpcError({
name: 'Invalid parameters',
code: -32602,
message: 'Invalid params',
}),
INVALID_REQUEST: new JsonRpcError({
name: 'Invalid request',
code: -32600,
message: 'Invalid request',
}),
IP_RATE_LIMIT_EXCEEDED: (methodName: string) =>
new JsonRpcError({
name: 'IP Rate limit exceeded',
code: -32605,
message: `IP Rate limit exceeded on ${methodName}`,
}),
MISSING_FROM_BLOCK_PARAM: new JsonRpcError({
name: 'Missing fromBlock parameter',
code: -32011,
message: 'Provided toBlock parameter without specifying fromBlock',
}),
MISSING_REQUIRED_PARAMETER: (index: number | string) =>
new JsonRpcError({
name: 'Missing required parameters',
code: -32602,
message: `Missing value for required parameter ${index}`,
}),
NONCE_TOO_LOW: (nonce, currentNonce) =>
new JsonRpcError({
name: 'Nonce too low',
code: 32001,
message: `Nonce too low. Provided nonce: ${nonce}, current nonce: ${currentNonce}`,
}),
NONCE_TOO_HIGH: (nonce, currentNonce) =>
new JsonRpcError({
name: 'Nonce too high',
code: 32002,
message: `Nonce too high. Provided nonce: ${nonce}, current nonce: ${currentNonce}`,
}),
NO_MINING_WORK: new JsonRpcError({
name: 'No mining work',
code: -32000,
message: 'No mining work available yet',
}),
PARSE_ERROR: new JsonRpcError({
name: 'Parse error',
code: -32700,
message: 'Unable to parse JSON',
}),
RANGE_TOO_LARGE: (blockRange: number) =>
new JsonRpcError({
name: 'Block range too large',
code: -32000,
message: `Exceeded maximum block range: ${blockRange}`,
}),
REQUEST_BEYOND_HEAD_BLOCK: (requested: number, latest: number) =>
new JsonRpcError({
name: 'Incorrect block',
code: -32000,
message: `Request beyond head block: requested ${requested}, head ${latest}`,
}),
REQUEST_TIMEOUT: new JsonRpcError({
name: 'Request timeout',
code: -32010,
message: 'Request timeout. Please try again.',
}),
RESOURCE_NOT_FOUND: (message = '') =>
new JsonRpcError({
name: 'Resource not found',
code: -32001,
message: `Requested resource not found. ${message}`,
}),
UNKNOWN_HISTORICAL_BALANCE: new JsonRpcError({
name: 'Unavailable balance',
code: -32007,
message: 'Historical balance data is available only after 15 minutes.',
}),
UNSUPPORTED_CHAIN_ID: (requested: string | number, current: string | number) =>
new JsonRpcError({
name: 'ChainId not supported',
code: -32000,
message: `ChainId (${requested}) not supported. The correct chainId is ${current}`,
}),
UNSUPPORTED_METHOD: new JsonRpcError({
name: 'Method not found',
code: -32601,
message: 'Unsupported JSON-RPC method',
}),
UNSUPPORTED_TRANSACTION_TYPE: new JsonRpcError({
name: 'Unsupported transaction type',
code: -32611,
message: 'Unsupported transaction type',
}),
VALUE_TOO_LOW: new JsonRpcError({
name: 'Value too low',
code: -32602,
message: 'Value below 10_000_000_000 wei which is 1 tinybar',
}),
Expand All @@ -186,7 +159,6 @@ export const predefined = {
}

return new JsonRpcError({
name: 'Invalid Contract Address',
code: -32012,
message: message,
});
Expand All @@ -198,7 +170,6 @@ export const predefined = {
}

return new JsonRpcError({
name: 'Non Existing Contract Address',
code: -32013,
message: message,
});
Expand All @@ -210,97 +181,80 @@ export const predefined = {
}

return new JsonRpcError({
name: 'Non Existing Account Address',
code: -32014,
message: message,
});
},
COULD_NOT_ESTIMATE_GAS_PRICE: new JsonRpcError({
name: 'Could not estimate gas price',
code: -32604,
message: 'Error encountered estimating the gas price',
}),
COULD_NOT_RETRIEVE_LATEST_BLOCK: new JsonRpcError({
name: 'Could not retrieve latest block',
code: -32607,
message: 'Error encountered retrieving latest block',
}),
MAX_SUBSCRIPTIONS: new JsonRpcError({
name: 'Exceeded maximum allowed subscriptions',
code: -32608,
message: 'Exceeded maximum allowed subscriptions',
}),
UNSUPPORTED_HISTORICAL_EXECUTION: (blockId: string) =>
new JsonRpcError({
name: 'Unsupported historical block request',
code: -32609,
message: `Unsupported historical block identifier encountered: ${blockId}`,
}),
UNSUPPORTED_OPERATION: (message: string) =>
new JsonRpcError({
name: 'Unsupported operation',
code: -32610,
message: `Unsupported operation. ${message}`,
}),
PAGINATION_MAX: (count: number) =>
new JsonRpcError({
name: 'Mirror Node pagination count range too large',
code: -32011,
message: `Exceeded maximum mirror node pagination count: ${count}`,
}),
MAX_BLOCK_SIZE: (count: number) =>
new JsonRpcError({
name: 'Block size too large',
code: -32000,
message: `Exceeded max transactions that can be returned in a block: ${count}`,
}),
UNKNOWN_BLOCK: (msg?: string | null) =>
new JsonRpcError({
name: 'Unknown block',
code: -39012,
message: msg || 'Unknown block',
}),
INVALID_BLOCK_RANGE: new JsonRpcError({
name: 'Invalid block range',
code: -39013,
message: 'Invalid block range',
}),
FILTER_NOT_FOUND: new JsonRpcError({
name: 'Filter not found',
code: -32001,
message: 'Filter not found',
}),
TRANSACTION_SIZE_TOO_BIG: (actualSize: string, expectedSize: string) =>
new JsonRpcError({
name: 'Transaction size too big',
code: -32201,
message: `Oversized data: transaction size ${actualSize}, transaction limit ${expectedSize}`,
}),
BATCH_REQUESTS_DISABLED: new JsonRpcError({
name: 'Batch requests disabled',
code: -32202,
message: 'Batch requests are disabled',
}),
BATCH_REQUESTS_AMOUNT_MAX_EXCEEDED: (amount: number, max: number) =>
new JsonRpcError({
name: 'Batch requests amount max exceeded',
code: -32203,
message: `Batch request amount ${amount} exceeds max ${max}`,
}),
WS_BATCH_REQUESTS_DISABLED: new JsonRpcError({
name: 'WS batch requests disabled',
code: -32205,
message: 'WS batch requests are disabled',
}),
WS_BATCH_REQUESTS_AMOUNT_MAX_EXCEEDED: (amount: number, max: number) =>
new JsonRpcError({
name: 'WS batch requests amount max exceeded',
code: -32206,
message: `Batch request amount ${amount} exceeds max ${max}`,
}),
INVALID_ARGUMENTS: (message: string) =>
new JsonRpcError({
name: 'Invalid argument',
code: -32000,
message: `Invalid arguments: ${message}`,
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/tests/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export default class RelayAssertions {
return await expect(method.apply(thisObj, args), `${error.message}`).to.eventually.be.rejected.and.satisfy(
(err) => {
if (!checkMessage) {
return err.code === error.code && err.name === error.name;
return err.code === error.code;
}
return err.code === error.code && err.name === error.name && err.message === error.message;
return err.code === error.code && err.message === error.message;
},
);
};
Expand Down
5 changes: 0 additions & 5 deletions packages/relay/tests/lib/errors/JsonRpcError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ describe('Errors', () => {
describe('JsonRpcError', () => {
it('Constructs correctly without request ID', () => {
const err = new JsonRpcError({
name: 'TestError',
code: -32999,
message: 'test error: foo',
data: 'some data',
});
expect(err.code).to.eq(-32999);
expect(err.name).to.eq('TestError');
expect(err.data).to.eq('some data');

// Check that request ID is *not* prefixed
Expand All @@ -41,17 +39,14 @@ describe('Errors', () => {
it('Constructs correctly with request ID', () => {
const err = new JsonRpcError(
{
name: 'TestError',
code: -32999,
message: 'test error: foo',
data: 'some data',
},
'abcd-1234',
);
expect(err.code).to.eq(-32999);
expect(err.name).to.eq('TestError');
expect(err.data).to.eq('some data');

// Check that request ID is prefixed
expect(err.message).to.eq('[Request ID: abcd-1234] test error: foo');
});
Expand Down
8 changes: 0 additions & 8 deletions packages/relay/tests/lib/eth/eth_call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ describe('@ethCall Eth Call spec', async function () {

const result = await ethImpl.call(callData, 'latest');

expect((result as JsonRpcError).name).to.equal('Non Existing Account Address');
expect((result as JsonRpcError).code).to.equal(-32014);
expect((result as JsonRpcError).message).to.equal(
`Non Existing Account Address: ${callData.from}. Expected an Account Address.`,
Expand Down Expand Up @@ -401,7 +400,6 @@ describe('@ethCall Eth Call spec', async function () {
const call: string | JsonRpcError = await ethImpl.call(ETH_CALL_REQ_ARGS, 'latest');

expect((call as JsonRpcError).code).to.equal(expectedError.code);
expect((call as JsonRpcError).name).to.equal(expectedError.name);
expect((call as JsonRpcError).message).to.equal(expectedError.message);
});

Expand All @@ -415,7 +413,6 @@ describe('@ethCall Eth Call spec', async function () {

expect(result).to.exist;
expect((result as JsonRpcError).code).to.equal(3);
expect((result as JsonRpcError).name).to.equal(undefined);
expect((result as JsonRpcError).message).to.equal(`execution reverted: ${defaultErrorMessageText}`);
expect((result as JsonRpcError).data).to.equal(defaultErrorMessageHex);
});
Expand Down Expand Up @@ -456,7 +453,6 @@ describe('@ethCall Eth Call spec', async function () {

expect(result).to.exist;
expect((result as JsonRpcError).code).to.equal(-32603);
expect((result as JsonRpcError).name).to.equal('Internal error');
expect((result as JsonRpcError).message).to.equal(
'Error invoking RPC: Invalid contractCallResponse from consensus-node: undefined',
);
Expand Down Expand Up @@ -603,7 +599,6 @@ describe('@ethCall Eth Call spec', async function () {
const result = await ethImpl.call(callData, 'latest');
expect(result).to.be.not.null;
expect((result as JsonRpcError).code).to.eq(-32605);
expect((result as JsonRpcError).name).to.eq('IP Rate limit exceeded');
});

it('eth_call with all fields but mirrorNode throws 400', async function () {
Expand All @@ -621,7 +616,6 @@ describe('@ethCall Eth Call spec', async function () {
const result = await ethImpl.call(callData, 'latest');
expect(result).to.be.not.null;
expect((result as JsonRpcError).code).to.eq(3);
expect((result as JsonRpcError).name).to.eq(undefined);
expect((result as JsonRpcError).message).to.contain(mockData.contractReverted._status.messages[0].message);
});

Expand Down Expand Up @@ -676,7 +670,6 @@ describe('@ethCall Eth Call spec', async function () {
sinon.assert.notCalled(sdkClientStub.submitContractCallQueryWithRetry);
expect(result).to.not.be.null;
expect((result as JsonRpcError).code).to.eq(3);
expect((result as JsonRpcError).name).to.eq(undefined);
expect((result as JsonRpcError).message).to.contain(mockData.contractReverted._status.messages[0].message);
});

Expand Down Expand Up @@ -707,7 +700,6 @@ describe('@ethCall Eth Call spec', async function () {

expect(result).to.exist;
expect((result as JsonRpcError).code).to.eq(3);
expect((result as JsonRpcError).name).to.eq(undefined);
expect((result as JsonRpcError).message).to.equal(`execution reverted: ${defaultErrorMessageText}`);
expect((result as JsonRpcError).data).to.equal(defaultErrorMessageHex);
});
Expand Down
2 changes: 0 additions & 2 deletions packages/relay/tests/lib/eth/eth_common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ describe('@ethCommon', async function () {
const result = Relay.eth().getWork();
expect(result).to.have.property('code');
expect(result.code).to.be.equal(-32601);
expect(result).to.have.property('name');
expect(result.name).to.be.equal('Method not found');
expect(result).to.have.property('message');
expect(result.message).to.be.equal('Unsupported JSON-RPC method');
});
Expand Down
Loading

0 comments on commit ef4c91e

Please sign in to comment.