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.
This commit is contained in:
Simon Larsen 2024-07-04 22:22:37 +01:00
parent 0e53e26695
commit cc3f003be5
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
2 changed files with 20 additions and 9 deletions

View File

@ -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 {

View File

@ -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);
}
}