Skip to content

Commit

Permalink
refactor: Improve error handling in FetchListAndProbe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simlarsen committed Jul 5, 2024
1 parent 9b86216 commit de7d06e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
13 changes: 12 additions & 1 deletion Common/Types/Exception/ApiException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
7 changes: 4 additions & 3 deletions Common/Utils/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions Ingestor/API/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ router.post(

// return the list of monitors to be monitored

logger.debug("Sending response");
return Response.sendEntityArrayResponse(
req,
res,
Expand Down
6 changes: 6 additions & 0 deletions Probe/Jobs/Monitor/FetchList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));
}
}
}
}

0 comments on commit de7d06e

Please sign in to comment.