Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

hand mid-stream asyncHandler failures straight to the error chain (#2845)

[._.]/ Adam Eivy15a872454917
diff --git a/server/lib/errorHandler.js b/server/lib/errorHandler.js
index 2bc3619793..9e1be4fb0f 100644
--- a/server/lib/errorHandler.js
+++ b/server/lib/errorHandler.js
@@ -111,6 +111,14 @@ export function asyncHandler(fn) {
const io = req.app.get('io');
const error = normalizeError(err);
+ // A handler that throws after it started streaming (SSE routes) has no
+ // envelope left to write. Hand the NORMALIZED error to the error chain —
+ // `errorMiddleware` logs/emits it exactly once and then delegates to
+ // Express's finalhandler, which destroys the socket. Passing the raw
+ // value would let a falsy throw (`throw null`) read as success and leave
+ // the request hanging.
+ if (res.headersSent) return next(error);
+
// Log the error (skip stack traces for upstream platform issues)
const route = `${req.method} ${routePath(req)}`;
const suffix = causeSuffix(error);
@@ -127,13 +135,7 @@ export function asyncHandler(fn) {
console.error(details ? `${logMsg}: ${JSON.stringify(details)}` : logMsg);
}
- // Read BEFORE sending — a successful write flips the flag. A handler that
- // throws after it started streaming (SSE routes) has no envelope left to
- // write, so hand off to Express's finalhandler to destroy the socket
- // instead of leaving the request hanging.
- const alreadyStreaming = res.headersSent;
- sendErrorResponse(res, error, { io });
- if (alreadyStreaming) next(err);
+ return sendErrorResponse(res, error, { io });
});
};
}
@@ -339,7 +341,7 @@ export function errorMiddleware(err, req, res, next) {
// BEFORE sending: a successful write flips it to true.
const alreadyStreaming = res.headersSent;
sendErrorResponse(res, error, { io });
- if (alreadyStreaming) next(err);
+ if (alreadyStreaming) next(error);
}
/**
diff --git a/server/lib/errorHandler.test.js b/server/lib/errorHandler.test.js
index 7b7505fefa..eac7b4fe21 100644
--- a/server/lib/errorHandler.test.js
+++ b/server/lib/errorHandler.test.js
@@ -224,6 +224,7 @@ describe('errorHandler.js', () => {
errorMiddleware(err, makeReq(), res, next);
expect(res.json).not.toHaveBeenCalled();
+ // The normalized error is forwarded, so a falsy throw can't read as success.
expect(next).toHaveBeenCalledWith(err);
});
@@ -385,8 +386,12 @@ describe('errorHandler.js', () => {
expect(body.error).toBe('kaboom');
});
- it('delegates to next(err) when the handler throws mid-stream', async () => {
- const { req, res } = makeReqRes(null);
+ // Mid-stream the envelope can't land, so the catch hands off to the error
+ // chain WITHOUT reporting locally — errorMiddleware logs/emits it once and
+ // then lets finalhandler destroy the socket.
+ it('delegates to next() without reporting when the handler throws mid-stream', async () => {
+ const io = { emit: vi.fn() };
+ const { req, res } = makeReqRes(io);
res.headersSent = true;
const next = vi.fn();
const boom = new ServerError('mid-stream', { status: 500 });
@@ -395,9 +400,23 @@ describe('errorHandler.js', () => {
await flushMicrotasks();
expect(res.json).not.toHaveBeenCalled();
+ expect(io.emit).not.toHaveBeenCalled();
expect(next).toHaveBeenCalledWith(boom);
});
+ // `throw null` must not reach next() as a falsy value — Express would read
+ // that as "no error" and keep routing, hanging the request.
+ it('normalizes a falsy throw before delegating mid-stream', async () => {
+ const { req, res } = makeReqRes(null);
+ res.headersSent = true;
+ const next = vi.fn();
+
+ await asyncHandler(async () => { throw null; })(req, res, next);
+ await flushMicrotasks();
+
+ expect(next).toHaveBeenCalledWith(expect.any(ServerError));
+ });
+
it('does not touch res when the wrapped handler resolves successfully', async () => {
const { req, res } = makeReqRes({ emit: vi.fn() });