From 633474bc78de5bc9a76913361f15f2d08057aea5 Mon Sep 17 00:00:00 2001 From: stefanbaxter Date: Thu, 23 Apr 2026 08:18:56 +0000 Subject: [PATCH] fix(cubejs): guard express error handler against already-sent responses The fallback error middleware called `res.status().send()` unconditionally, which threw `Cannot set headers after they are sent to the client` when upstream middleware (e.g. cube's api-gateway handler) had already written the response. The thrown error masked the original stack. Bail out via `next(err)` when `res.headersSent` so the default express handler can finish tearing down the connection. Co-Authored-By: Claude Opus 4.7 (1M context) --- services/cubejs/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/cubejs/index.js b/services/cubejs/index.js index ba37c869..a27f0af1 100644 --- a/services/cubejs/index.js +++ b/services/cubejs/index.js @@ -109,6 +109,10 @@ if (String(CUBEJS_SQL_API) === "true") { app.use((err, req, res, next) => { console.error(err.stack); + if (res.headersSent) { + return next(err); + } + res.status(err.status || 500).send(err.message); });