From 5b60ad9ea769573881b780ca405061100b1cc49d Mon Sep 17 00:00:00 2001 From: Antoine Arlaud Date: Wed, 29 Nov 2023 23:07:07 +0100 Subject: [PATCH] fix: add missing error handling event --- lib/client/index.ts | 9 +++++++++ lib/common/http/request.ts | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/client/index.ts b/lib/client/index.ts index 663ca08df..320d95019 100644 --- a/lib/client/index.ts +++ b/lib/client/index.ts @@ -13,6 +13,15 @@ import { systemCheckHandler } from './routesHandler/systemCheckHandler'; import { ClientOpts } from './types/client'; import { processStartUpHooks } from './hooks/startup/processHooks'; +process.on('uncaughtException', (error) => { + logger.error( + { msg: error.message, stackTrace: error.stack }, + 'Uncaught exception:', + error.message, + ); + process.exit(1); +}); + export const main = async (clientOpts: ClientOpts) => { try { logger.info({ version }, 'running in client mode'); diff --git a/lib/common/http/request.ts b/lib/common/http/request.ts index def8a2041..e95d54ec9 100644 --- a/lib/common/http/request.ts +++ b/lib/common/http/request.ts @@ -178,6 +178,27 @@ export const makeStreamingRequestToDownstream = ( `Non 2xx HTTP Code Received`, ); } + + response.on('error', (error) => { + if (retries > 0) { + logger.warn( + { msg: localRequest.url }, + `Downstream Response failed. Retrying after 500ms...`, + ); + setTimeout(() => { + resolve( + makeStreamingRequestToDownstream(localRequest, retries - 1), + ); + }, 500); // Wait for 0.5 second before retrying + } else { + logger.error( + { error }, + `Error getting response from downstream. Giving up after ${MAX_RETRY} retries.`, + ); + reject(error); + } + }); + resolve(response); }, );