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-05 10:18:03 +01:00
parent 9b862162b6
commit de7d06e5d7
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
4 changed files with 23 additions and 4 deletions

View File

@ -2,7 +2,18 @@ import Exception from "./Exception";
import ExceptionCode from "./ExceptionCode";
export default class APIException extends Exception {
public constructor(message: string) {
private _error: Error | null = null;
public get error(): Error | null {
return this._error || null;
}
public set error(v: Error | null) {
this._error = v;
}
public constructor(message: string, error?: Error) {
super(ExceptionCode.APIException, message);
if (error) {
this.error = error;
}
}
}

View File

@ -322,9 +322,10 @@ export default class API {
// get url from error
const url: string = error?.config?.url || "";
throw new APIException(`
Error occurred while making request to ${url}. ${error.message}
`);
throw new APIException(
`Error occurred while making request to ${url}.`,
error,
);
}
public static getFriendlyErrorMessage(error: AxiosError | Error): string {

View File

@ -340,6 +340,7 @@ router.post(
// return the list of monitors to be monitored
logger.debug("Sending response");
return Response.sendEntityArrayResponse(
req,
res,

View File

@ -7,6 +7,7 @@ import HTTPMethod from "Common/Types/API/HTTPMethod";
import HTTPResponse from "Common/Types/API/HTTPResponse";
import URL from "Common/Types/API/URL";
import OneUptimeDate from "Common/Types/Date";
import APIException from "Common/Types/Exception/ApiException";
import { JSONArray } from "Common/Types/JSON";
import ProbeMonitorResponse from "Common/Types/Probe/ProbeMonitorResponse";
import Sleep from "Common/Types/Sleep";
@ -116,6 +117,11 @@ export default class FetchListAndProbe {
} catch (err) {
logger.error("Error in fetching monitor list");
logger.error(err);
if (err instanceof APIException) {
logger.error("API Exception Error");
logger.error(JSON.stringify(err.error, null, 2));
}
}
}
}