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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([issue-760]): emit context from errorMiddleware + assert sync-throw bodies
[._.]/ Adam Eivye51513ba596c
diff --git a/server/lib/errorHandler.js b/server/lib/errorHandler.jsindex dd84b63cbc..78c37a003d 100644--- a/server/lib/errorHandler.js+++ b/server/lib/errorHandler.js@@ -266,16 +266,23 @@ export function errorMiddleware(err, req, res, next) {console.error(logMsg);}+ // Sanitize once and share with both the socket event and the HTTP body so+ // the two channels can't drift on what's stripped — mirrors asyncHandler.+ const safeContext = sanitizeContext(error.context);+// Emit Socket.IO eventif (io) {- emitErrorEvent(io, error);+ emitErrorEvent(io, error, safeContext);}- // Send response+ // Send response. Include `context` (e.g. a service's `details` / `modelId`)+ // when present, matching the envelope asyncHandler emits — synchronous+ // handlers that throw a ServerError land here, not in asyncHandler.res.status(error.status).json({error: error.message,code: error.code,- timestamp: error.timestamp+ timestamp: error.timestamp,+ ...(safeContext && Object.keys(safeContext).length > 0 && { context: safeContext })});}diff --git a/server/lib/errorHandler.test.js b/server/lib/errorHandler.test.jsindex 2c08c2893d..2fa2d12a75 100644--- a/server/lib/errorHandler.test.js+++ b/server/lib/errorHandler.test.js@@ -3,7 +3,8 @@ import {ServerError,normalizeError,emitErrorEvent,- errorEvents+ errorEvents,+ errorMiddleware} from './errorHandler.js';describe('errorHandler.js', () => {@@ -161,6 +162,40 @@ describe('errorHandler.js', () => {});});+ describe('errorMiddleware', () => {+ const makeRes = () => {+ const res = {};+ res.status = vi.fn(() => res);+ res.json = vi.fn(() => res);+ return res;+ };+ const makeReq = () => ({ method: 'GET', originalUrl: '/x', app: { get: () => null } });++ it('emits the standard envelope and includes non-empty sanitized context', () => {+ const res = makeRes();+ errorMiddleware(+ new ServerError('boom', { status: 404, context: { modelId: 'm', apiKey: 'secret' } }),+ makeReq(),+ res,+ () => {}+ );+ expect(res.status).toHaveBeenCalledWith(404);+ const body = res.json.mock.calls[0][0];+ expect(body.error).toBe('boom');+ expect(body.code).toBe('NOT_FOUND');+ expect(body.timestamp).toBeDefined();+ // context is forwarded but sensitive keys are stripped.+ expect(body.context).toEqual({ modelId: 'm' });+ });++ it('omits context when it is empty', () => {+ const res = makeRes();+ errorMiddleware(new ServerError('nope', { status: 400 }), makeReq(), res, () => {});+ const body = res.json.mock.calls[0][0];+ expect(body).not.toHaveProperty('context');+ });+ });+describe('emitErrorEvent', () => {it('should emit error event to errorEvents', () => {const listener = vi.fn();diff --git a/server/routes/imageGen.test.js b/server/routes/imageGen.test.jsindex ad026854b7..d25bd202fe 100644--- a/server/routes/imageGen.test.js+++ b/server/routes/imageGen.test.js@@ -1,6 +1,7 @@import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';import express from 'express';import { request } from '../lib/testHelper.js';+import { errorMiddleware } from '../lib/errorHandler.js';import imageGenRoutes from './imageGen.js';vi.mock('../services/imageGen/index.js', () => ({@@ -90,6 +91,7 @@ describe('Image Gen Routes', () => {app = express();app.use(express.json());app.use('/api/image-gen', imageGenRoutes);+ app.use(errorMiddleware);vi.clearAllMocks();});@@ -524,6 +526,8 @@ describe('Image Gen Routes', () => {imageGen.attachSseClient.mockReturnValueOnce(false);const response = await request(app).get('/api/image-gen/missing-job/events');expect(response.status).toBe(404);+ expect(response.body.error).toMatch(/not found/i);+ expect(response.body.code).toBe('NOT_FOUND');expect(imageGen.attachSseClient).toHaveBeenCalledWith('missing-job', expect.anything());});diff --git a/server/routes/videoTimeline.test.js b/server/routes/videoTimeline.test.jsindex 2255d019a0..985a52caad 100644--- a/server/routes/videoTimeline.test.js+++ b/server/routes/videoTimeline.test.js@@ -1,6 +1,7 @@import { describe, it, expect, vi, beforeEach } from 'vitest';import express from 'express';import { request } from '../lib/testHelper.js';+import { errorMiddleware } from '../lib/errorHandler.js';vi.mock('../services/videoTimeline/local.js', () => ({listProjects: vi.fn(),@@ -22,6 +23,7 @@ describe('videoTimeline routes', () => {app = express();app.use(express.json());app.use('/api/video-timeline', videoTimelineRoutes);+ app.use(errorMiddleware);vi.clearAllMocks();});@@ -136,6 +138,8 @@ describe('videoTimeline routes', () => {svc.attachSseClient.mockReturnValue(false);const r = await request(app).get('/api/video-timeline/unknown/events');expect(r.status).toBe(404);+ expect(r.body.error).toMatch(/not found/i);+ expect(r.body.code).toBe('NOT_FOUND');});});