Skip to content

Commit 8ef3c38

Browse files
authored
feat: gracefully handle HTML error responses from remote Mirror Node server (#3962)
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
1 parent 2c47363 commit 8ef3c38

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,16 @@ export class MirrorNodeClient {
378378
(data) => {
379379
// if the data is not valid, just return it to stick to the current behaviour
380380
if (data) {
381-
return JSONBigInt.parse(data);
381+
try {
382+
return JSONBigInt.parse(data);
383+
} catch (error) {
384+
this.logger.warn(
385+
`${requestDetails.formattedRequestId} Failed to parse response data from Mirror Node: ${error}`,
386+
);
387+
}
382388
}
383389

390+
// Return raw data so response can be processed properly by subsequent operations.
384391
return data;
385392
},
386393
];

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ describe('MirrorNodeClient', async function () {
204204
}
205205
});
206206
}
207+
208+
it('should gracefully handle HTML error responses', async () => {
209+
// Simulate Mirror Node returning HTML error page
210+
mock
211+
.onGet('accounts')
212+
.reply(
213+
502,
214+
`<!DOCTYPE html><html><head><title>502 Server Error</title></head><body>Error: Server Error</body></html>`,
215+
);
216+
await expect(mirrorNodeInstance.get('accounts', 'accounts', requestDetails))
217+
.to.eventually.be.rejectedWith('Request failed with status code 502')
218+
.and.have.property('statusCode', 502);
219+
});
220+
221+
it('should gracefully handle empty error responses', async () => {
222+
// Simulate Mirror Node returning HTML error page
223+
mock.onGet('accounts').reply(503, ``);
224+
await expect(mirrorNodeInstance.get('accounts', 'accounts', requestDetails))
225+
.to.eventually.be.rejectedWith('Request failed with status code 503')
226+
.and.have.property('statusCode', 503);
227+
});
207228
});
208229

209230
it('Can extract the account number out of an account pagination next link url', async () => {

0 commit comments

Comments
 (0)