Skip to content

Commit

Permalink
Merge pull request #674 from snyk/fix/add-response-error-handler
Browse files Browse the repository at this point in the history
fix: add missing error handling event [ED-319]
  • Loading branch information
aarlaud authored Nov 29, 2023
2 parents 2132567 + 5b60ad9 commit aed1605
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
21 changes: 21 additions & 0 deletions lib/common/http/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
);
Expand Down

0 comments on commit aed1605

Please sign in to comment.