Skip to content

Commit 008e7f4

Browse files
authored
Cherry-Pick: resolveEntityType error handling improvement inside promises (#1216) (#1217)
resolveEntityType error handling improvement inside promises (#1216) resolveEntityType uses promises, if the promises returned an exception, it was not being bubbled/propagated, but instead it was crashing the app, the most common case is when the mirror node returns a 504 (timeout). now its returning null Signed-off-by: Alfredo Gutierrez <[email protected]>
1 parent 4cb6a1b commit 008e7f4

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/relay/src/lib/clients/mirrorNodeClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ export class MirrorNodeClient {
719719
}));
720720

721721
if (searchableTypes.find(t => t === constants.TYPE_CONTRACT)) {
722-
const contract = await this.getContract(entityIdentifier, requestId);
722+
const contract = await this.getContract(entityIdentifier, requestId).catch(() => {return null;});
723723
if (contract) {
724724
const response = {
725725
type: constants.TYPE_CONTRACT,
@@ -733,12 +733,12 @@ export class MirrorNodeClient {
733733
let data;
734734
try {
735735
const promises = [
736-
searchableTypes.find(t => t === constants.TYPE_ACCOUNT) ? buildPromise(this.getAccount(entityIdentifier, requestId)) : Promise.reject(),
736+
searchableTypes.find(t => t === constants.TYPE_ACCOUNT) ? buildPromise(this.getAccount(entityIdentifier, requestId).catch(() => {return null;})) : Promise.reject(),
737737
];
738738

739739
// only add long zero evm addresses for tokens as they do not refer to actual contract addresses but rather encoded entity nums
740740
if (entityIdentifier.startsWith(constants.LONG_ZERO_PREFIX)) {
741-
promises.push(searchableTypes.find(t => t === constants.TYPE_TOKEN) ? buildPromise(this.getTokenById(`0.0.${parseInt(entityIdentifier, 16)}`, requestId)) : Promise.reject());
741+
promises.push(searchableTypes.find(t => t === constants.TYPE_TOKEN) ? buildPromise(this.getTokenById(`0.0.${parseInt(entityIdentifier, 16)}`, requestId).catch(() => {return null;})) : Promise.reject());
742742
}
743743

744744
// maps the promises with indices of the promises array

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,6 +3298,32 @@ describe('Eth calls using MirrorNode', async function () {
32983298
expect(result).to.equal("0x00");
32993299
});
33003300

3301+
it('eth_call with all fields but mirrorNode throws 504 (timeout) on pre-check', async function () {
3302+
3303+
const timeoutAddress = "0x00000000000000000000000000000000000004e2";
3304+
const timeoutContract = "0x00000000000000000000000000000000000004e3";
3305+
restMock.onGet(`contracts/${timeoutAddress}`).reply(504);
3306+
restMock.onGet(`accounts/${timeoutContract}`).reply(504);
3307+
3308+
const callData = {
3309+
...defaultCallData,
3310+
"from": timeoutAddress,
3311+
"to": timeoutContract,
3312+
"data": contractCallData,
3313+
"gas": maxGasLimit
3314+
};
3315+
web3Mock.onPost('contracts/call', {...callData, estimate: false}).reply(200, {result: `0x00`});
3316+
3317+
let error;
3318+
try {
3319+
const result = await ethImpl.call(callData, 'latest');
3320+
} catch (e) {
3321+
error = e;
3322+
}
3323+
expect(error).to.be.not.null;
3324+
expect(error.message).to.equal("Non Existing Account Address: 0x00000000000000000000000000000000000004e2. Expected an Account Address.");
3325+
});
3326+
33013327
it('eth_call with all fields, but mirror node throws NOT_SUPPORTED', async function () {
33023328
const callData = {
33033329
...defaultCallData,

0 commit comments

Comments
 (0)