early returns

This commit is contained in:
Alex Holliday committed 2025-08-29 09:22:02 -07:00
1 parent ba757b9aa4
commit 44ba6b4d0f
1 file changed
+38 -23
@@ -253,6 +253,11 @@ class NetworkService {
handleError: true, // Enable error handling handleError: true, // Enable error handling
}); });
const dockerResponse = {
monitorId: monitor._id,
type: monitor.type,
};
const containers = await docker.listContainers({ all: true }); const containers = await docker.listContainers({ all: true });
// Normalize input: strip leading slashes and convert to lowercase for comparison // Normalize input: strip leading slashes and convert to lowercase for comparison
@@ -276,37 +281,47 @@ class NetworkService {
// Select container based on priority // Select container based on priority
let targetContainer = exactIdMatch || exactNameMatch || partialIdMatch; let targetContainer = exactIdMatch || exactNameMatch || partialIdMatch;
// Log warning if ambiguous matches exist // Return negative response if no container
if (targetContainer && this.logger) { if (!targetContainer) {
const matchTypes = []; this.logger.warn({
if (exactIdMatch) matchTypes.push("exact ID"); message: `No container found for "${monitor.url}".`,
if (exactNameMatch) matchTypes.push("exact name"); service: this.SERVICE_NAME,
if (partialIdMatch && !exactIdMatch) matchTypes.push("partial ID"); method: "requestDocker",
details: { url: monitor.url },
});
if (matchTypes.length > 1) { dockerResponse.code = 404;
this.logger.warn({ dockerResponse.status = false;
message: `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`, dockerResponse.message = this.stringService.dockerNotFound;
service: this.SERVICE_NAME, return dockerResponse;
method: "requestDocker",
});
}
} }
if (!targetContainer) { // Return negative response if ambiguous matches exist
throw new Error(this.stringService.dockerNotFound); const matchTypes = [];
if (exactIdMatch) matchTypes.push("exact ID");
if (exactNameMatch) matchTypes.push("exact name");
if (partialIdMatch && !exactIdMatch) matchTypes.push("partial ID");
if (matchTypes.length > 1) {
this.logger.warn({
message: `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`,
service: this.SERVICE_NAME,
method: "requestDocker",
details: { url: monitor.url },
});
dockerResponse.status = 404;
dockerResponse.status = false;
dockerResponse.message = `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`;
return dockerResponse;
} }
const container = docker.getContainer(targetContainer.Id); const container = docker.getContainer(targetContainer.Id);
const { response, responseTime, error } = await this.timeRequest(() => container.inspect()); const { response, responseTime, error } = await this.timeRequest(() => container.inspect());
const dockerResponse = { dockerResponse.responseTime = responseTime;
monitorId: monitor._id, dockerResponse.status = response?.State?.Status === "running" ? true : false;
type: monitor.type, dockerResponse.code = 200;
responseTime, dockerResponse.message = "Docker container status fetched successfully";
status: response?.State?.Status === "running" ? true : false,
code: 200,
message: "Docker container status fetched successfully",
};
if (error) { if (error) {
dockerResponse.status = false; dockerResponse.status = false;