From c8116fc9e8f6a30e249bc0ece49c99c86019ebdd Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 22 Jan 2023 14:23:14 -0800 Subject: [PATCH] Better error output for docker integration --- src/pages/api/docker/stats/[...service].js | 24 ++++++++++----------- src/pages/api/docker/status/[...service].js | 8 +++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/pages/api/docker/stats/[...service].js b/src/pages/api/docker/stats/[...service].js index 82604f8c7..84e90065f 100644 --- a/src/pages/api/docker/stats/[...service].js +++ b/src/pages/api/docker/stats/[...service].js @@ -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 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"}, }); } } diff --git a/src/pages/api/docker/status/[...service].js b/src/pages/api/docker/status/[...service].js index fa54e6f3b..f232eb98e 100644 --- a/src/pages/api/docker/status/[...service].js +++ b/src/pages/api/docker/status/[...service].js @@ -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"}, }); } }