diff --git a/auth/src/__tests__/health.test.ts b/auth/src/__tests__/health.test.ts index efb66fd..96352f4 100644 --- a/auth/src/__tests__/health.test.ts +++ b/auth/src/__tests__/health.test.ts @@ -19,9 +19,18 @@ describe('Auth health endpoint', () => { } res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ status: 'ok', db: 'reachable' })); - } catch { + } catch (err) { + // Mirror src/index.ts: log the error and include the message in the + // response body so /health 503s are diagnosable from pod logs. + console.error( + '[auth /health] DB probe failed:', + err instanceof Error ? `${err.name}: ${err.message}` : err, + ); + const detail = err instanceof Error ? err.message : 'unknown error'; res.writeHead(503, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ status: 'error', db: 'unreachable' })); + res.end( + JSON.stringify({ status: 'error', db: 'unreachable', error: detail }), + ); } return; } @@ -76,7 +85,10 @@ describe('Auth health endpoint', () => { close(); equal(status, 503); - equal(body, '{"status":"error","db":"unreachable"}'); + const parsed = JSON.parse(body); + equal(parsed.status, 'error'); + equal(parsed.db, 'unreachable'); + equal(parsed.error, 'connection refused'); }); it('returns 503 with db=unreachable when query times out', async () => { @@ -95,7 +107,14 @@ describe('Auth health endpoint', () => { close(); equal(status, 503); - equal(body, '{"status":"error","db":"unreachable"}'); + const parsed = JSON.parse(body); + equal(parsed.status, 'error'); + equal(parsed.db, 'unreachable'); + // The query promise rejects with a synthetic 'timeout' error; the + // Promise.race wrapper also rejects with 'DB timeout'. The body should + // surface whichever error was thrown — accept either to stay robust. + equal(typeof parsed.error, 'string'); + equal(parsed.error.length > 0, true); }); it('returns a terminal response for unknown paths (no hang)', async () => { diff --git a/auth/src/index.ts b/auth/src/index.ts index 71448e1..4cd753a 100644 --- a/auth/src/index.ts +++ b/auth/src/index.ts @@ -21,9 +21,19 @@ const server = createServer(async (req, res) => { } res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ status: "ok", db: "reachable" })); - } catch { + } catch (err) { + // Log the actual error so /health 503s are diagnosable from pod logs + // (CAR-1276: UAT auth was crashlooping with no log output beyond the + // initial "listening on port 3001" line because this catch was empty). + console.error( + "[auth /health] DB probe failed:", + err instanceof Error ? `${err.name}: ${err.message}` : err, + ); + const detail = err instanceof Error ? err.message : "unknown error"; res.writeHead(503, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ status: "error", db: "unreachable" })); + res.end( + JSON.stringify({ status: "error", db: "unreachable", error: detail }), + ); } return; }