Better error output for docker integration

pull/871/head
Michael Shamoon 2 years ago
parent c74b46712b
commit c8116fc9e8

@ -1,16 +1,18 @@
import Docker from "dockerode";
import getDockerArguments from "utils/config/docker";
import createLogger from "utils/logger";
const logger = createLogger("dockerStatsService");
export default async function handler(req, res) {
const { service } = req.query;
const [containerName, containerServer] = service;
if (!containerName && !containerServer) {
res.status(400).send({
return res.status(400).send({
error: "docker query parameters are required",
});
return;
}
try {
@ -23,10 +25,9 @@ export default async function handler(req, res) {
// bad docker connections can result in a <Buffer ...> object?
// in any case, this ensures the result is the expected array
if (!Array.isArray(containers)) {
res.status(500).send({
return res.status(500).send({
error: "query failed",
});
return;
}
const containerNames = containers.map((container) => container.Names[0].replace(/^\//, ""));
@ -36,10 +37,9 @@ export default async function handler(req, res) {
const container = docker.getContainer(containerName);
const stats = await container.stats({ stream: false });
res.status(200).json({
return res.status(200).json({
stats,
});
return;
}
// Try with a service deployed in Docker Swarm, if enabled
@ -61,19 +61,19 @@ export default async function handler(req, res) {
const container = docker.getContainer(taskContainerId);
const stats = await container.stats({ stream: false });
res.status(200).json({
return res.status(200).json({
stats,
});
return;
}
}
res.status(200).send({
return res.status(200).send({
error: "not found",
});
} catch {
res.status(500).send({
error: {message: "Unknown error"},
} catch (e) {
logger.error(e);
return res.status(500).send({
error: {message: e?.message ?? "Unknown error"},
});
}
}

@ -1,6 +1,9 @@
import Docker from "dockerode";
import getDockerArguments from "utils/config/docker";
import createLogger from "utils/logger";
const logger = createLogger("dockerStatusService");
export default async function handler(req, res) {
const { service } = req.query;
@ -68,9 +71,10 @@ export default async function handler(req, res) {
return res.status(200).send({
error: "not found",
});
} catch {
} catch (e) {
logger.error(e);
return res.status(500).send({
error: "unknown error",
error: {message: e?.message ?? "Unknown error"},
});
}
}

Loading…
Cancel
Save