From cc3f003be58db4a1bdb6b00d89915c7426d8d286 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Thu, 4 Jul 2024 22:22:37 +0100 Subject: [PATCH] refactor: Improve error handling in FetchListAndProbe This commit refactors the FetchListAndProbe class to improve error handling. It adds try-catch blocks around the main logic and logs any errors that occur using the logger. Additionally, it includes a catch block to handle any errors thrown during the fetchListAndProbe function. This change ensures that errors are properly handled and logged, preventing potential issues with the monitoring process. --- Common/Utils/API.ts | 4 +++- Probe/Jobs/Monitor/FetchList.ts | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Common/Utils/API.ts b/Common/Utils/API.ts index 644b90bb3b..6bb38f95b9 100644 --- a/Common/Utils/API.ts +++ b/Common/Utils/API.ts @@ -322,7 +322,9 @@ export default class API { // get url from error const url: string = error?.config?.url || ""; - throw new APIException(`URL ${url ? url + " " : ""}is not available.`); + throw new APIException(` + Error occurred while making request to ${url}. ${error.message} + `); } public static getFriendlyErrorMessage(error: AxiosError | Error): string { diff --git a/Probe/Jobs/Monitor/FetchList.ts b/Probe/Jobs/Monitor/FetchList.ts index 0aa84905fb..e71d0cb5da 100644 --- a/Probe/Jobs/Monitor/FetchList.ts +++ b/Probe/Jobs/Monitor/FetchList.ts @@ -26,20 +26,29 @@ export default class FetchListAndProbe { // eslint-disable-next-line no-constant-condition while (true) { - const runTime: Date = OneUptimeDate.getCurrentDate(); + try { + const runTime: Date = OneUptimeDate.getCurrentDate(); - logger.debug(`Probing monitors ${this.workerName}`); + logger.debug(`Probing monitors ${this.workerName}`); - await this.fetchListAndProbe(); + await this.fetchListAndProbe(); - logger.debug(`Probing monitors ${this.workerName} complete`); + logger.debug(`Probing monitors ${this.workerName} complete`); - // if rumTime + 5 seconds is in the future, then this fetchLst either errored out or had no monitors in the list. Either way, wait for 5 seconds and proceed. + // if rumTime + 5 seconds is in the future, then this fetchLst either errored out or had no monitors in the list. Either way, wait for 5 seconds and proceed. - const twoSecondsAdded: Date = OneUptimeDate.addRemoveSeconds(runTime, 2); + const twoSecondsAdded: Date = OneUptimeDate.addRemoveSeconds( + runTime, + 2, + ); - if (OneUptimeDate.isInTheFuture(twoSecondsAdded)) { - logger.debug(`Worker ${this.workerName} is waiting for 2 seconds`); + if (OneUptimeDate.isInTheFuture(twoSecondsAdded)) { + logger.debug(`Worker ${this.workerName} is waiting for 2 seconds`); + await Sleep.sleep(2000); + } + } catch (err) { + logger.error(`Error in worker ${this.workerName}`); + logger.error(err); await Sleep.sleep(2000); } }