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

fix: preserve the original failure stack in async route logs (#9743)

[._.]/ Adam Eivy7c984f847dd0
diff --git a/server/lib/errorHandler.js b/server/lib/errorHandler.js
index ec1a0878e4..d44c072fe7 100644
--- a/server/lib/errorHandler.js
+++ b/server/lib/errorHandler.js
@@ -201,7 +201,10 @@ export function asyncHandler(fn) {
// Expected high-volume 404s (e.g. speculative media-job archive
// lookups) — already classified as benign; don't pollute server logs.
} else if (error.status >= 500) {
- console.error(logMsg, error.stack ? error.stack : '');
+ // Use the original Error's stack (from before normalization) when available
+ // so diagnostics point to the actual throwing function, not normalizeError.
+ const stack = error.originalStack || error.stack;
+ console.error(logMsg, stack ? stack : '');
} else {
// An empty `[]`/`{}` is truthy but summarizes to nothing — gate on the
// summary so the line can't end in a dangling colon.
@@ -312,6 +315,11 @@ export function normalizeError(err) {
{ status, code, context, responseMessage: err.responseMessage },
);
if (normalized.message !== err.message) normalized.originalMessage = err.message;
+ // Preserve the original Error's stack for local server logging; the normalized
+ // ServerError's stack starts from the normalizeError call. Callers log this
+ // field when available instead of the normalized stack so diagnostics point
+ // to the actual throwing function, not the error handler.
+ normalized.originalStack = err.stack;
return normalized;
}
diff --git a/server/lib/errorHandler.test.js b/server/lib/errorHandler.test.js
index 8b011190e1..38b0272ca6 100644
--- a/server/lib/errorHandler.test.js
+++ b/server/lib/errorHandler.test.js
@@ -570,6 +570,33 @@ describe('errorHandler.js', () => {
expect(line).not.toContain('a: b');
expect(stack).toBe(error.stack);
});
+
+ // Regression: a plain Error thrown from a service function must log its
+ // original stack location, not the normalizeError wrapper's stack. Use a
+ // named synthetic throwing function so the location is stable and visible
+ // in the stack trace.
+ it('preserves the original Error stack when normalizing a 500 error through asyncHandler', async () => {
+ const { req, res } = makeReqRes(null);
+
+ // Define a named function that throws a plain Error (not a ServerError).
+ function syntheticServiceFunction() {
+ throw new Error('Service error from syntheticServiceFunction');
+ }
+
+ await asyncHandler(async () => {
+ syntheticServiceFunction();
+ })(req, res, vi.fn());
+ await flushMicrotasks();
+
+ expect(errorSpy).toHaveBeenCalledTimes(1);
+ const [, stack] = errorSpy.mock.calls[0];
+
+ // The logged stack must contain the original throwing function's name,
+ // not normalizeError. This proves the originalStack field was captured
+ // and used for logging.
+ expect(stack).toContain('syntheticServiceFunction');
+ expect(stack).not.toContain('normalizeError');
+ });
});
describe('sendErrorResponse', () => {