From c301c2f9d66dae08029113429460bb6425c9c182 Mon Sep 17 00:00:00 2001 From: SaelixCode Date: Fri, 20 Feb 2026 22:29:03 -0500 Subject: [PATCH] refactor: add guard clause for empty output and enhance JSON parsing in DockerController --- backend/src/services/DockerController.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/src/services/DockerController.ts b/backend/src/services/DockerController.ts index e788f1a0..96517195 100644 --- a/backend/src/services/DockerController.ts +++ b/backend/src/services/DockerController.ts @@ -46,6 +46,11 @@ class DockerController { } }); + // Guard clause for empty output (stack has no containers) + if (!stdout || stdout.trim() === '') { + return []; + } + // Robust JSON parsing - handle both JSON array and newline-separated JSON objects // Docker Compose v2 may return either format depending on version interface ComposeContainer { @@ -61,8 +66,8 @@ class DockerController { const parsed = JSON.parse(stdout); containers = Array.isArray(parsed) ? parsed : [parsed]; } catch { - // Fallback: parse newline-separated JSON objects - const lines = stdout.trim().split('\n'); + // Fallback: parse newline-separated JSON objects, filtering out empty lines + const lines = stdout.trim().split('\n').filter(line => line.trim() !== ''); containers = lines.map(line => JSON.parse(line) as ComposeContainer); }