Skip to content

Commit 433a5e5

Browse files
ebadieregeorgi-l95
andauthored
Return meaningful JSON RPC errors (#1213) (#1234)
* return error msg * fix tests and code smells * fix tests * handle msg in error class --------- Signed-off-by: georgi-l95 <[email protected]> Signed-off-by: ebadiere <[email protected]> Co-authored-by: Georgi Lazarov <[email protected]>
1 parent ea51719 commit 433a5e5

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

packages/relay/src/lib/errors/JsonRpcError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const predefined = {
7474
'INTERNAL_ERROR': (message = '') => new JsonRpcError({
7575
name: 'Internal error',
7676
code: -32603,
77-
message: message === '' ? 'Unknown error invoking RPC' : `Error invoking RPC: ${message}`
77+
message: message === '' || undefined ? 'Unknown error invoking RPC' : `Error invoking RPC: ${message}`
7878
}),
7979
'INVALID_PARAMETER': (index: number | string, message: string) => new JsonRpcError({
8080
name: 'Invalid parameter',

packages/relay/src/lib/eth.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ export class EthImpl implements Eth {
997997
if (e instanceof JsonRpcError) {
998998
return e;
999999
}
1000-
return predefined.INTERNAL_ERROR();
1000+
return predefined.INTERNAL_ERROR(e.message.toString());
10011001
}
10021002
}
10031003

@@ -1056,7 +1056,7 @@ export class EthImpl implements Eth {
10561056
if (e instanceof JsonRpcError) {
10571057
return e;
10581058
}
1059-
return predefined.INTERNAL_ERROR();
1059+
return predefined.INTERNAL_ERROR(e.message.toString());
10601060
}
10611061
}
10621062

@@ -1107,7 +1107,10 @@ export class EthImpl implements Eth {
11071107
return await this.callConsensusNode(call, gas, requestId);
11081108
} catch (e: any) {
11091109
this.logger.error(e, `${requestIdPrefix} Failed to successfully submit eth_call`);
1110-
return e instanceof JsonRpcError ? e : predefined.INTERNAL_ERROR();
1110+
if (e instanceof JsonRpcError) {
1111+
return e;
1112+
}
1113+
return predefined.INTERNAL_ERROR(e.message.toString());
11111114
}
11121115
}
11131116

@@ -1139,7 +1142,10 @@ export class EthImpl implements Eth {
11391142
return await this.callConsensusNode(call, gas, requestId);
11401143
}
11411144
this.logger.error(e, `${requestIdPrefix} Failed to successfully submit eth_call`);
1142-
return e instanceof JsonRpcError ? e : predefined.INTERNAL_ERROR();
1145+
if (e instanceof JsonRpcError) {
1146+
return e;
1147+
}
1148+
return predefined.INTERNAL_ERROR(e.message.toString());
11431149
}
11441150
}
11451151

@@ -1179,7 +1185,7 @@ export class EthImpl implements Eth {
11791185
if (e instanceof JsonRpcError) {
11801186
return e;
11811187
}
1182-
return predefined.INTERNAL_ERROR();
1188+
return predefined.INTERNAL_ERROR(e.message.toString());
11831189
}
11841190
}
11851191

@@ -1586,8 +1592,7 @@ export class EthImpl implements Eth {
15861592
e,
15871593
`${requestIdPrefix} Failed to retrieve contract result details for contract address ${to} at timestamp=${timestamp}`
15881594
);
1589-
1590-
throw predefined.INTERNAL_ERROR();
1595+
throw predefined.INTERNAL_ERROR(e.message.toString());
15911596
});
15921597
}
15931598

@@ -1756,8 +1761,7 @@ export class EthImpl implements Eth {
17561761
if (error instanceof JsonRpcError) {
17571762
throw error;
17581763
}
1759-
1760-
return predefined.INTERNAL_ERROR();
1764+
return predefined.INTERNAL_ERROR(error.message.toString());
17611765
}
17621766

17631767
/**************************************************

packages/relay/tests/lib/eth.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,8 +3475,8 @@ describe('Eth calls using MirrorNode', async function () {
34753475
await ethImpl.sendRawTransaction(txHash);
34763476
} catch (e) {
34773477
hasError = true;
3478-
expect(e.code).to.equal(predefined.INTERNAL_ERROR().code);
3479-
expect(e.message).to.equal(predefined.INTERNAL_ERROR().message);
3478+
expect(e.code).to.equal(predefined.INTERNAL_ERROR(e.message).code);
3479+
expect(`Error invoking RPC: ${e.message}`).to.equal(predefined.INTERNAL_ERROR(e.message).message);
34803480
}
34813481
expect(hasError).to.be.true;
34823482
});

packages/server/tests/acceptance/rpc_batch1.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,24 @@ describe('@api-batch-1 RPC Server Acceptance Tests', function () {
498498
Assertions.transactionReceipt(res, mirrorResult);
499499
});
500500

501+
it('@release should fail to execute "eth_getTransactionReceipt" for hash of London transaction', async function () {
502+
const gasPrice = await relay.gasPrice(requestId);
503+
const transaction = {
504+
...defaultLondonTransactionData,
505+
to: mirrorContract.evm_address,
506+
nonce: await relay.getAccountNonce('0x' + accounts[2].address, requestId),
507+
maxFeePerGas: gasPrice,
508+
maxPriorityFeePerGas: gasPrice
509+
};
510+
511+
const signedTx = await accounts[2].wallet.signTransaction(transaction);
512+
try {
513+
await relay.sendRawTransaction(signedTx+"11", requestId);
514+
} catch (error) {
515+
expect(`Error invoking RPC: ${error.message}`).to.equal(predefined.INTERNAL_ERROR(error.message).message);
516+
}
517+
});
518+
501519
it('should execute "eth_getTransactionReceipt" for non-existing hash', async function () {
502520
const res = await relay.call('eth_getTransactionReceipt', [NON_EXISTING_TX_HASH], requestId);
503521
expect(res).to.be.null;
@@ -584,7 +602,11 @@ describe('@api-batch-1 RPC Server Acceptance Tests', function () {
584602
gasPrice: await relay.gasPrice(requestId)
585603
};
586604
const signedTx = await accounts[2].wallet.signTransaction(transaction);
587-
await relay.callFailing('eth_sendRawTransaction', [signedTx], predefined.INTERNAL_ERROR(), requestId);
605+
try {
606+
await relay.sendRawTransaction(signedTx, requestId);
607+
} catch (error) {
608+
expect(`Error invoking RPC: ${error.message}`).to.equal(predefined.INTERNAL_ERROR(error.message).message);
609+
}
588610
});
589611

590612
it('should fail "eth_sendRawTransaction" for Legacy 2930 transactions (with gas price too low)', async function () {

packages/server/tests/clients/relayClient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export default class RelayClient {
6161
this.logger.trace(`${requestIdPrefix} [POST] to relay '${methodName}' with params [${params}] returned ${JSON.stringify(res)}`);
6262
Assertions.expectedError();
6363
} catch (err) {
64+
if (expectedRpcError.name == "Internal error"){
65+
expectedRpcError = predefined.INTERNAL_ERROR(err.message);
66+
}
6467
Assertions.jsonRpcError(err, expectedRpcError);
6568
}
6669
}

0 commit comments

Comments
 (0)