resolve remaining TS errors

This commit is contained in:
Alex Holliday committed 2026-03-03 19:34:28 +00:00
1 parent a0ea50d745
commit f79a87f98a
3 files changed
+280 -123

No files matched your search

+16 -1
View File
@@ -145,7 +145,22 @@ export const initializeServices = async ({
const teamsRepository = new MongoTeamsRepository(); const teamsRepository = new MongoTeamsRepository();
const maintenanceWindowsRepository = new MongoMaintenanceWindowsRepository(); const maintenanceWindowsRepository = new MongoMaintenanceWindowsRepository();
const networkService = new NetworkService(axios, got, https, jmespath, GameDig, ping, logger, Docker, net, settingsService, grpc, protoLoader); const networkService = new NetworkService(
axios,
got,
https,
jmespath,
GameDig as unknown as {
query: (options: { type: string; host: string; port?: number }) => Promise<{ ping?: number } & { [key: string]: unknown }>;
},
ping,
logger,
Docker,
net,
settingsService,
grpc,
protoLoader
);
const emailService = new EmailService(settingsService, fs, path, compile, mjml2html, nodemailer, logger); const emailService = new EmailService(settingsService, fs, path, compile, mjml2html, nodemailer, logger);
const notificationMessageBuilder = new NotificationMessageBuilder(); const notificationMessageBuilder = new NotificationMessageBuilder();
@@ -7,6 +7,10 @@ import type {
PageSpeedStatusPayload, PageSpeedStatusPayload,
HttpStatusPayload, HttpStatusPayload,
HardwareStatusPayload, HardwareStatusPayload,
PingStatusPayload,
DockerStatusPayload,
PortStatusPayload,
GameStatusPayload,
} from "@/types/index.js"; } from "@/types/index.js";
import type { AxiosStatic } from "axios"; import type { AxiosStatic } from "axios";
import { AppError } from "@/utils/AppError.js"; import { AppError } from "@/utils/AppError.js";
@@ -34,8 +38,25 @@ interface BuildStatusResponseArgs<T> {
export interface INetworkService { export interface INetworkService {
readonly serviceName: string; readonly serviceName: string;
requestStatus(monitor: Monitor): Promise<MonitorStatusResponse>; requestStatus(
requestWebhook(type: string, url: string, body: any): Promise<{ type: string; status: boolean; code: number; message: string; payload?: unknown }>; monitor: Monitor
): Promise<
MonitorStatusResponse<
| PingStatusPayload
| HttpStatusPayload
| PageSpeedStatusPayload
| HardwareStatusPayload
| DockerStatusPayload
| PortStatusPayload
| GameStatusPayload
| GrpcStatusPayload
>
>;
requestWebhook(
type: string,
url: string,
body: unknown
): Promise<{ type: string; status: boolean; code: number; message: string; payload?: unknown }>;
requestPagerDuty(args: { message: string; routingKey: string; monitorUrl: string }): Promise<boolean>; requestPagerDuty(args: { message: string; routingKey: string; monitorUrl: string }): Promise<boolean>;
requestMatrix(args: { homeserverUrl: string; accessToken: string; roomId: string; message: string }): Promise<{ requestMatrix(args: { homeserverUrl: string; accessToken: string; roomId: string; message: string }): Promise<{
status: boolean; status: boolean;
@@ -62,16 +83,16 @@ class NetworkService implements INetworkService {
private axios: AxiosStatic; private axios: AxiosStatic;
private got: Got; private got: Got;
private https: any; private https: typeof import("https");
private jmespath: any; private jmespath: { search: (data: unknown, expression: string) => unknown };
private GameDig: any; private GameDig: { query: (options: { type: string; host: string; port?: number }) => Promise<{ ping?: number } & { [key: string]: unknown }> };
private ping: any; private ping: typeof import("ping");
private logger: ILogger; private logger: ILogger;
private Docker: any; private Docker: typeof import("dockerode");
private net: any; private net: typeof import("net");
private settingsService: ISettingsService; private settingsService: ISettingsService;
private grpc: any; private grpc: typeof import("@grpc/grpc-js");
private protoLoader: any; private protoLoader: typeof import("@grpc/proto-loader");
private buildStatusResponse = <T>({ private buildStatusResponse = <T>({
monitor, monitor,
@@ -106,7 +127,7 @@ class NetworkService implements INetworkService {
statusResponse.responseTime = error.timings?.phases?.total ?? 0; statusResponse.responseTime = error.timings?.phases?.total ?? 0;
statusResponse.timings = error.timings; statusResponse.timings = error.timings;
} }
return { ...statusResponse, ...(overrides ?? {}) }; return { ...statusResponse, ...(overrides ?? {}) } as MonitorStatusResponse<T>;
} }
return { return {
@@ -124,22 +145,22 @@ class NetworkService implements INetworkService {
expectedValue, expectedValue,
extracted, extracted,
...(overrides ?? {}), ...(overrides ?? {}),
}; } as MonitorStatusResponse<T>;
}; };
constructor( constructor(
axios: AxiosStatic, axios: AxiosStatic,
got: Got, got: Got,
https: any, https: typeof import("https"),
jmespath: any, jmespath: { search: (data: unknown, expression: string) => unknown },
GameDig: any, GameDig: { query: (options: { type: string; host: string; port?: number }) => Promise<{ ping?: number } & { [key: string]: unknown }> },
ping: any, ping: typeof import("ping"),
logger: ILogger, logger: ILogger,
Docker: any, Docker: typeof import("dockerode"),
net: any, net: typeof import("net"),
settingsService: ISettingsService, settingsService: ISettingsService,
grpc: any, grpc: typeof import("@grpc/grpc-js"),
protoLoader: any protoLoader: typeof import("@grpc/proto-loader")
) { ) {
this.TYPE_PING = "ping"; this.TYPE_PING = "ping";
this.TYPE_HTTP = "http"; this.TYPE_HTTP = "http";
@@ -193,7 +214,20 @@ class NetworkService implements INetworkService {
} }
// Main entry point // Main entry point
async requestStatus(monitor: Monitor): Promise<MonitorStatusResponse> { async requestStatus(
monitor: Monitor
): Promise<
MonitorStatusResponse<
| PingStatusPayload
| HttpStatusPayload
| PageSpeedStatusPayload
| HardwareStatusPayload
| DockerStatusPayload
| PortStatusPayload
| GameStatusPayload
| GrpcStatusPayload
>
> {
const type = monitor?.type || "unknown"; const type = monitor?.type || "unknown";
switch (type) { switch (type) {
case this.TYPE_PING: case this.TYPE_PING:
@@ -217,7 +251,7 @@ class NetworkService implements INetworkService {
} }
} }
private async requestPing(monitor: Monitor): Promise<MonitorStatusResponse> { private async requestPing(monitor: Monitor): Promise<MonitorStatusResponse<PingStatusPayload>> {
try { try {
if (!monitor?.url) { if (!monitor?.url) {
throw new Error("Monitor URL is required"); throw new Error("Monitor URL is required");
@@ -237,30 +271,27 @@ class NetworkService implements INetworkService {
throw new Error("Ping failed - no result returned"); throw new Error("Ping failed - no result returned");
} }
const pingResponse = this.buildStatusResponse({ const pingResponse = this.buildStatusResponse<PingStatusPayload>({
monitor, monitor,
payload: response, payload: response as PingStatusPayload,
overrides: { overrides: {
status: (response as { alive?: boolean })?.alive ?? false, status: (response as { alive?: boolean })?.alive ?? false,
code: 200, code: 200,
message: "Success", message: "Success",
responseTime: (response as { time?: number })?.time ?? 0, responseTime: (response as { time?: number })?.time ?? 0,
payload: response, payload: response as PingStatusPayload,
}, } as MonitorStatusResponseOverrides<PingStatusPayload>,
}); });
if (error) {
pingResponse.status = false;
pingResponse.code = this.PING_ERROR;
pingResponse.message = "Ping failed";
return pingResponse;
}
return pingResponse; return pingResponse;
} catch (err: any) { } catch (err: unknown) {
err.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
err.method = "requestPing"; throw new AppError({
throw err; message: originalMessage || "Error performing ping",
service: this.SERVICE_NAME,
method: "requestPing",
details: { url: monitor.url },
});
} }
} }
@@ -279,7 +310,7 @@ class NetworkService implements INetworkService {
if (!url) { if (!url) {
throw new Error("Monitor URL is required"); throw new Error("Monitor URL is required");
} }
const config: Record<string, any> = { const config: Record<string, unknown> = {
headers: secret ? { Authorization: `Bearer ${secret}` } : undefined, headers: secret ? { Authorization: `Bearer ${secret}` } : undefined,
}; };
@@ -293,7 +324,7 @@ class NetworkService implements INetworkService {
const response = await this.got(url, config); const response = await this.got(url, config);
let payload: any; let payload: T | string | undefined;
const contentType = response.headers["content-type"]; const contentType = response.headers["content-type"];
if (contentType && contentType.includes("application/json")) { if (contentType && contentType.includes("application/json")) {
@@ -381,19 +412,23 @@ class NetworkService implements INetworkService {
} }
} }
return httpResponse; return httpResponse;
} catch (err: any) { } catch (err: unknown) {
if (err.name === "HTTPError" || err.name === "RequestError") { if (err instanceof HTTPError || err instanceof RequestError) {
httpResponse.code = err?.response?.statusCode || this.NETWORK_ERROR; httpResponse.code = err.response?.statusCode || this.NETWORK_ERROR;
httpResponse.status = false; httpResponse.status = false;
httpResponse.message = err?.response?.statusCode || err.message; httpResponse.message = String(err.response?.statusCode) || err.message;
httpResponse.responseTime = err?.timings?.phases?.total || 0; httpResponse.responseTime = err.timings?.phases?.total || 0;
httpResponse.payload = null; httpResponse.payload = null;
httpResponse.timings = err?.timings || {}; httpResponse.timings = err.timings || undefined;
return httpResponse; return httpResponse;
} }
err.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
err.method = "requestHttp"; throw new AppError({
throw err; message: originalMessage || "Error performing HTTP request",
service: this.SERVICE_NAME,
method: "requestHttp",
details: { url: monitor.url },
});
} }
} }
@@ -419,24 +454,32 @@ class NetworkService implements INetworkService {
...monitor, ...monitor,
url: pageSpeedUrl, url: pageSpeedUrl,
}); });
} catch (err: any) { } catch (err: unknown) {
err.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
err.method = "requestPageSpeed"; throw new AppError({
throw err; message: originalMessage || "Error performing PageSpeed request",
service: this.SERVICE_NAME,
method: "requestPageSpeed",
details: { url: monitor.url },
});
} }
} }
private async requestHardware(monitor: Monitor): Promise<MonitorStatusResponse<HardwareStatusPayload>> { private async requestHardware(monitor: Monitor): Promise<MonitorStatusResponse<HardwareStatusPayload>> {
try { try {
return await this.requestHttp<HardwareStatusPayload>(monitor); return await this.requestHttp<HardwareStatusPayload>(monitor);
} catch (err: any) { } catch (err: unknown) {
err.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
err.method = "requestHardware"; throw new AppError({
throw err; message: originalMessage || "Error performing hardware request",
service: this.SERVICE_NAME,
method: "requestHardware",
details: { url: monitor.url },
});
} }
} }
private async requestDocker(monitor: Monitor): Promise<MonitorStatusResponse> { private async requestDocker(monitor: Monitor): Promise<MonitorStatusResponse<DockerStatusPayload>> {
try { try {
if (!monitor.url) { if (!monitor.url) {
throw new Error("Monitor URL is required"); throw new Error("Monitor URL is required");
@@ -446,7 +489,7 @@ class NetworkService implements INetworkService {
socketPath: "/var/run/docker.sock", socketPath: "/var/run/docker.sock",
}); });
const dockerResponse = this.buildStatusResponse({ const dockerResponse = this.buildStatusResponse<DockerStatusPayload>({
monitor, monitor,
overrides: { overrides: {
status: false, status: false,
@@ -455,17 +498,17 @@ class NetworkService implements INetworkService {
}, },
}); });
const containers = await docker.listContainers({ all: true }); const containers = await docker.listContainers();
// Normalize input: strip leading slashes and convert to lowercase for comparison // Normalize input: strip leading slashes and convert to lowercase for comparison
const normalizedInput = monitor.url.replace(/^\/+/, "").toLowerCase(); const normalizedInput = monitor.url.replace(/^\/+/, "").toLowerCase();
// Priority-based matching to avoid ambiguity: // Priority-based matching to avoid ambiguity:
// 1. Exact full ID match (64-char) // 1. Exact full ID match (64-char)
const exactIdMatch = containers.find((c: any) => c.Id.toLowerCase() === normalizedInput); const exactIdMatch = containers.find((c) => c.Id.toLowerCase() === normalizedInput);
// 2. Exact container name match (case-insensitive) // 2. Exact container name match (case-insensitive)
const exactNameMatch = containers.find((c: any) => const exactNameMatch = containers.find((c) =>
c.Names.some((name: string) => { c.Names.some((name: string) => {
const cleanName = name.replace(/^\/+/, "").toLowerCase(); const cleanName = name.replace(/^\/+/, "").toLowerCase();
return cleanName === normalizedInput; return cleanName === normalizedInput;
@@ -473,7 +516,7 @@ class NetworkService implements INetworkService {
); );
// 3. Partial ID match (fallback for backwards compatibility) // 3. Partial ID match (fallback for backwards compatibility)
const partialIdMatch = containers.find((c: any) => c.Id.toLowerCase().startsWith(normalizedInput)); const partialIdMatch = containers.find((c) => c.Id.toLowerCase().startsWith(normalizedInput));
// Select container based on priority // Select container based on priority
const targetContainer = exactIdMatch || exactNameMatch || partialIdMatch; const targetContainer = exactIdMatch || exactNameMatch || partialIdMatch;
@@ -512,9 +555,8 @@ class NetworkService implements INetworkService {
} }
const container = docker.getContainer(targetContainer.Id); const container = docker.getContainer(targetContainer.Id);
const { response, responseTime, error }: { response?: any; responseTime?: number; error?: any } = await this.timeRequest(() => const { response, responseTime, error }: { response?: { State: { Status: string } } | null; responseTime: number; error: unknown } =
container.inspect() await this.timeRequest(() => container.inspect());
);
dockerResponse.responseTime = responseTime; dockerResponse.responseTime = responseTime;
dockerResponse.status = response?.State?.Status === "running" ? true : false; dockerResponse.status = response?.State?.Status === "running" ? true : false;
@@ -523,20 +565,28 @@ class NetworkService implements INetworkService {
if (error) { if (error) {
dockerResponse.status = false; dockerResponse.status = false;
dockerResponse.code = error.statusCode || this.NETWORK_ERROR; dockerResponse.code =
dockerResponse.message = error.reason || "Failed to fetch Docker container information"; (error && typeof error === "object" && "statusCode" in error ? (error as { statusCode?: number }).statusCode : undefined) ||
this.NETWORK_ERROR;
dockerResponse.message =
(error && typeof error === "object" && "reason" in error ? (error as { reason?: string }).reason : undefined) ||
"Failed to fetch Docker container information";
return dockerResponse; return dockerResponse;
} }
return dockerResponse; return dockerResponse;
} catch (err: any) { } catch (err: unknown) {
err.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
err.method = "requestDocker"; throw new AppError({
throw err; message: originalMessage || "Error performing Docker request",
service: this.SERVICE_NAME,
method: "requestDocker",
details: { url: monitor.url },
});
} }
} }
private async requestPort(monitor: Monitor): Promise<MonitorStatusResponse> { private async requestPort(monitor: Monitor): Promise<MonitorStatusResponse<PortStatusPayload>> {
try { try {
const { url, port } = monitor; const { url, port } = monitor;
if (!port) { if (!port) {
@@ -563,14 +613,14 @@ class NetworkService implements INetworkService {
reject(new Error("Connection timeout")); reject(new Error("Connection timeout"));
}); });
socket.on("error", (err: any) => { socket.on("error", (err: unknown) => {
socket.destroy(); socket.destroy();
reject(err); reject(err);
}); });
}); });
}); });
const portResponse = this.buildStatusResponse({ const portResponse = this.buildStatusResponse<PortStatusPayload>({
monitor, monitor,
overrides: { overrides: {
code: 200, code: 200,
@@ -588,18 +638,22 @@ class NetworkService implements INetworkService {
} }
return portResponse; return portResponse;
} catch (error: any) { } catch (err: unknown) {
error.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
error.method = "requestTCP"; throw new AppError({
throw error; message: originalMessage || "Error performing port check",
service: this.SERVICE_NAME,
method: "requestPort",
details: { url: monitor.url, port: monitor.port },
});
} }
} }
private async requestGame(monitor: Monitor): Promise<MonitorStatusResponse> { private async requestGame(monitor: Monitor): Promise<MonitorStatusResponse<GameStatusPayload>> {
try { try {
const { url, port, gameId } = monitor; const { url, port, gameId } = monitor;
const gameResponse = this.buildStatusResponse({ const gameResponse = this.buildStatusResponse<GameStatusPayload>({
monitor, monitor,
overrides: { overrides: {
code: 200, code: 200,
@@ -612,13 +666,14 @@ class NetworkService implements INetworkService {
type: gameId ?? "unknown", type: gameId ?? "unknown",
host: url, host: url,
port: port ?? 0, port: port ?? 0,
}).catch((error: any) => { }).catch((error: unknown): undefined => {
this.logger.warn({ this.logger.warn({
message: error.message, message: error instanceof Error ? error.message : String(error),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "requestGame", method: "requestGame",
details: { url, port, gameId }, details: { url, port, gameId },
}); });
return undefined;
}); });
if (!state) { if (!state) {
@@ -628,16 +683,20 @@ class NetworkService implements INetworkService {
return gameResponse; return gameResponse;
} }
gameResponse.responseTime = state.ping; gameResponse.responseTime = state.ping ?? 0;
gameResponse.payload = state; gameResponse.payload = state as GameStatusPayload;
return gameResponse; return gameResponse;
} catch (error: any) { } catch (err: unknown) {
error.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
error.method = "requestPing"; throw new AppError({
throw error; message: originalMessage || "Error performing game server check",
service: this.SERVICE_NAME,
method: "requestGame",
details: { url: monitor.url, port: monitor.port, gameId: monitor.gameId },
});
} }
} }
private async requestGrpc(monitor: Monitor): Promise<MonitorStatusResponse> { private async requestGrpc(monitor: Monitor): Promise<MonitorStatusResponse<GrpcStatusPayload>> {
try { try {
const { url, port, ignoreTlsErrors } = monitor; const { url, port, ignoreTlsErrors } = monitor;
const grpcServiceName = monitor.grpcServiceName || ""; const grpcServiceName = monitor.grpcServiceName || "";
@@ -661,7 +720,21 @@ class NetworkService implements INetworkService {
defaults: true, defaults: true,
oneofs: true, oneofs: true,
}); });
const grpcObject = this.grpc.loadPackageDefinition(packageDefinition); const grpcObject = this.grpc.loadPackageDefinition(packageDefinition) as unknown as {
grpc: {
health: {
v1: {
Health: new (
target: string,
credentials: unknown
) => {
Check: (request: { service: string }, options: { deadline: Date }, callback: (err: unknown, response: unknown) => void) => void;
close: () => void;
};
};
};
};
};
const healthService = grpcObject.grpc.health.v1.Health; const healthService = grpcObject.grpc.health.v1.Health;
let credentials; let credentials;
@@ -678,7 +751,7 @@ class NetworkService implements INetworkService {
const TIMEOUT_MS = 10000; const TIMEOUT_MS = 10000;
const deadline = new Date(Date.now() + TIMEOUT_MS); const deadline = new Date(Date.now() + TIMEOUT_MS);
const grpcResponse = this.buildStatusResponse({ const grpcResponse = this.buildStatusResponse<GrpcStatusPayload>({
monitor, monitor,
overrides: { overrides: {
status: false, status: false,
@@ -689,24 +762,30 @@ class NetworkService implements INetworkService {
const { response, responseTime, error } = await this.timeRequest<GrpcStatusPayload>(() => { const { response, responseTime, error } = await this.timeRequest<GrpcStatusPayload>(() => {
return new Promise<GrpcStatusPayload>((resolve, reject) => { return new Promise<GrpcStatusPayload>((resolve, reject) => {
client.Check({ service: grpcServiceName }, { deadline }, (err: any, response: any) => { client.Check({ service: grpcServiceName }, { deadline }, (err: unknown, response: unknown) => {
client.close(); client.close();
if (err) { if (err) {
const grpcErr = err as { code?: number; details?: string; message?: string };
const payload: GrpcStatusPayload = { const payload: GrpcStatusPayload = {
grpcStatusCode: err.code ?? -1, grpcStatusCode: grpcErr.code ?? -1,
grpcStatusName: this.getGrpcStatusName(err.code), grpcStatusName: this.getGrpcStatusName(grpcErr.code ?? -1),
serviceName: grpcServiceName, serviceName: grpcServiceName,
servingStatus: "UNKNOWN", servingStatus: "UNKNOWN",
}; };
const grpcError = new AppError({ message: err.details || err.message, service: this.SERVICE_NAME, method: "requestGrpc" }); const grpcError = new AppError({
(grpcError as any).grpcPayload = payload; message: grpcErr.details || grpcErr.message || "gRPC error",
(grpcError as any).grpcCode = err.code; service: this.SERVICE_NAME,
method: "requestGrpc",
}) as AppError & { grpcPayload?: GrpcStatusPayload; grpcCode?: number };
grpcError.grpcPayload = payload;
grpcError.grpcCode = grpcErr.code;
reject(grpcError); reject(grpcError);
return; return;
} }
const servingStatus = response?.status || "UNKNOWN"; const resp = response as { status?: string } | undefined;
const servingStatus = resp?.status ?? "UNKNOWN";
resolve({ resolve({
grpcStatusCode: 0, grpcStatusCode: 0,
grpcStatusName: "OK", grpcStatusName: "OK",
@@ -718,13 +797,13 @@ class NetworkService implements INetworkService {
}); });
if (error) { if (error) {
const grpcError = error as any; const grpcError = error as AppError & { grpcPayload?: GrpcStatusPayload; grpcCode?: number };
const payload = grpcError.grpcPayload as GrpcStatusPayload | undefined; const payload = grpcError.grpcPayload;
grpcResponse.status = false; grpcResponse.status = false;
grpcResponse.code = grpcError.grpcCode ?? this.NETWORK_ERROR; grpcResponse.code = grpcError.grpcCode ?? this.NETWORK_ERROR;
grpcResponse.message = grpcError.message || "gRPC health check failed"; grpcResponse.message = grpcError.message ?? "gRPC health check failed";
grpcResponse.responseTime = responseTime; grpcResponse.responseTime = responseTime;
grpcResponse.payload = payload || null; grpcResponse.payload = payload ?? null;
return grpcResponse; return grpcResponse;
} }
@@ -740,10 +819,14 @@ class NetworkService implements INetworkService {
grpcResponse.payload = grpcPayload; grpcResponse.payload = grpcPayload;
return grpcResponse; return grpcResponse;
} catch (err: any) { } catch (err: unknown) {
err.service = this.SERVICE_NAME; const originalMessage = err instanceof Error ? err.message : String(err);
err.method = "requestGrpc"; throw new AppError({
throw err; message: originalMessage || "Error performing gRPC health check",
service: this.SERVICE_NAME,
method: "requestGrpc",
details: { url: monitor.url, port: monitor.port, grpcServiceName: monitor.grpcServiceName },
});
} }
} }
@@ -782,7 +865,7 @@ class NetworkService implements INetworkService {
} }
// Other network requests unrelated to monitoring: // Other network requests unrelated to monitoring:
async requestWebhook(type: string, url: string, body: any) { async requestWebhook(type: string, url: string, body: unknown) {
try { try {
const response = await this.axios.post(url, body, { const response = await this.axios.post(url, body, {
headers: { headers: {
@@ -797,19 +880,29 @@ class NetworkService implements INetworkService {
message: `Successfully sent ${type} notification`, message: `Successfully sent ${type} notification`,
payload: response.data, payload: response.data,
}; };
} catch (error: any) { } catch (err: unknown) {
this.logger.warn({ this.logger.warn({
message: error.message, message: err instanceof Error ? err.message : String(err),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "requestWebhook", method: "requestWebhook",
}); });
if (err && typeof err === "object" && "response" in err) {
const axiosError = err as { response?: { status?: number; data?: unknown } };
return {
type: "webhook",
status: false,
code: axiosError.response?.status ?? this.NETWORK_ERROR,
message: `Failed to send ${type} notification`,
payload: axiosError.response?.data,
};
}
return { return {
type: "webhook", type: "webhook",
status: false, status: false,
code: error.response?.status || this.NETWORK_ERROR, code: this.NETWORK_ERROR,
message: `Failed to send ${type} notification`, message: `Failed to send ${type} notification`,
payload: error.response?.data,
}; };
} }
} }
@@ -829,11 +922,17 @@ class NetworkService implements INetworkService {
if (response?.data?.status !== "success") return false; if (response?.data?.status !== "success") return false;
return true; return true;
} catch (error: any) { } catch (err: unknown) {
error.details = error.response?.data; const originalMessage = err instanceof Error ? err.message : String(err);
error.service = this.SERVICE_NAME;
error.method = "requestPagerDuty"; throw new AppError({
throw error; message: originalMessage || "Error sending PagerDuty notification",
service: this.SERVICE_NAME,
method: "requestPagerDuty",
details: {
responseData: err && typeof err === "object" && "response" in err ? (err as { response?: { data?: unknown } }).response?.data : undefined,
},
});
} }
} }
@@ -867,18 +966,35 @@ class NetworkService implements INetworkService {
code: response.status, code: response.status,
message: "Successfully sent Matrix notification", message: "Successfully sent Matrix notification",
}; };
} catch (error: any) { } catch (err: unknown) {
if (err instanceof Error) {
this.logger.warn({
message: err.message,
service: this.SERVICE_NAME,
method: "requestMatrix",
});
if (err && typeof err === "object" && "response" in err) {
const axiosError = err as { response?: { status?: number; data?: unknown } };
return {
status: false,
code: axiosError.response?.status || this.NETWORK_ERROR,
message: "Failed to send Matrix notification",
payload: axiosError.response?.data,
};
}
}
this.logger.warn({ this.logger.warn({
message: error.message, message: String(err),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "requestMatrix", method: "requestMatrix",
}); });
return { return {
status: false, status: false,
code: error.response?.status || this.NETWORK_ERROR, code: this.NETWORK_ERROR,
message: "Failed to send Matrix notification", message: "Failed to send Matrix notification",
payload: error.response?.data,
}; };
} }
} }
@@ -7,6 +7,12 @@ import type {
Check, Check,
HardwareStatusPayload, HardwareStatusPayload,
PageSpeedStatusPayload, PageSpeedStatusPayload,
PingStatusPayload,
HttpStatusPayload,
DockerStatusPayload,
PortStatusPayload,
GameStatusPayload,
GrpcStatusPayload,
CheckSnapshot, CheckSnapshot,
MonitorStats, MonitorStats,
} from "@/types/index.js"; } from "@/types/index.js";
@@ -17,7 +23,17 @@ export interface IStatusService {
updateRunningStats({ monitor, networkResponse }: { monitor: Monitor; networkResponse: any }): Promise<boolean>; updateRunningStats({ monitor, networkResponse }: { monitor: Monitor; networkResponse: any }): Promise<boolean>;
handleIncidentForCheck(check: any, monitor: Monitor, action: any, errorContext?: string): Promise<void>; handleIncidentForCheck(check: any, monitor: Monitor, action: any, errorContext?: string): Promise<void>;
updateMonitorStatus( updateMonitorStatus(
statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>, statusResponse: MonitorStatusResponse<
| PingStatusPayload
| HttpStatusPayload
| PageSpeedStatusPayload
| HardwareStatusPayload
| DockerStatusPayload
| PortStatusPayload
| GameStatusPayload
| GrpcStatusPayload
| undefined
>,
check: Check check: Check
): Promise<StatusChangeResult>; ): Promise<StatusChangeResult>;
} }
@@ -205,7 +221,17 @@ export class StatusService implements IStatusService {
}; };
updateMonitorStatus = async ( updateMonitorStatus = async (
statusResponse: MonitorStatusResponse<PageSpeedStatusPayload | HardwareStatusPayload | undefined>, statusResponse: MonitorStatusResponse<
| PingStatusPayload
| HttpStatusPayload
| PageSpeedStatusPayload
| HardwareStatusPayload
| DockerStatusPayload
| PortStatusPayload
| GameStatusPayload
| GrpcStatusPayload
| undefined
>,
check: Check check: Check
): Promise<StatusChangeResult> => { ): Promise<StatusChangeResult> => {
try { try {