Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): forward allowlisted registry-integration errors at the global handler
Jose Ramirezd8d972cde517
diff --git a/control-api/src/app.ts b/control-api/src/app.tsindex a8599f314..538e887f7 100644--- a/control-api/src/app.ts+++ b/control-api/src/app.ts@@ -26,6 +26,11 @@ import { createRegistryRouter } from './routes/registry.js'import { createRpcAccessRouter } from './routes/rpc-access/index.js'import { memberRegistrationErrorResponse } from './services/memberRegistrationErrors.js'+// Only errors we construct with these codes are ever forwarded verbatim by the+// global handler (see below). Keeps the blast radius tight: every other+// status-less or non-allowlisted error still collapses to a 500.+const FORWARDABLE_INTEGRATION_CODES = new Set(['registry_unavailable', 'registry_integration_error'])+export function createApp(gateway: K8sGateway) {const app = express()app.set('trust proxy', 1)@@ -185,6 +190,36 @@ export function createApp(gateway: K8sGateway) {return}+ // Allowlisted registry-integration errors carry a safe code + message we set+ // ourselves (RegistryUnavailableError → 503; the 401 remap → 502). Forward+ // them so the marketplace shows a clear message instead of a raw 500. The+ // message is safe because only our own constructors set these codes.+ const integrationCode = (err as { code?: unknown }).code+ const integrationStatus = (err as { status?: unknown }).status+ if (+ typeof integrationStatus === 'number' &&+ (integrationStatus === 502 || integrationStatus === 503) &&+ typeof integrationCode === 'string' &&+ FORWARDABLE_INTEGRATION_CODES.has(integrationCode)+ ) {+ log.warn(+ {+ event: 'forwarded_registry_integration_error',+ correlationId,+ status: integrationStatus,+ code: integrationCode,+ err: err instanceof Error ? err.message : String(err),+ },+ 'forwarded registry integration error'+ )+ res.status(integrationStatus).json({+ error: integrationCode,+ message: err instanceof Error ? err.message : 'Registry integration error',+ correlationId,+ })+ return+ }+log.error({event: 'unhandled_error',diff --git a/control-api/test/app.registryCatalogUnavailable.test.ts b/control-api/test/app.registryCatalogUnavailable.test.tsnew file mode 100644index 000000000..3cd09a987--- /dev/null+++ b/control-api/test/app.registryCatalogUnavailable.test.ts@@ -0,0 +1,82 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import express from 'express'+import request from 'supertest'+import { createApp } from '../src/app.js'+import { RegistryUnavailableError } from '../src/services/registryClient.js'+import { MockGateway } from './mockGateway.js'++// Bypass the control-ui auth gate so the supertest request reaches the real+// catalog route (and the real global error handler) without a session cookie.+vi.mock('../src/middleware/controlUIAuth.js', () => ({+ requireAuthForControlUI: (+ req: express.Request,+ _res: express.Response,+ next: express.NextFunction+ ) => {+ ;(req as unknown as { adminAuth: unknown }).adminAuth = {+ sub: 'admin-1',+ role: 'admin',+ typ: 'user',+ }+ next()+ },+}))++// Partial-mock the registry client: keep every real export (RegistryProxyError,+// applyPublishScope, RegistryUnavailableError, …) so the admin router still+// loads, and override only the two hot catalog reads so we can make them throw.+const registryClient = vi.hoisted(() => ({+ searchEntries: vi.fn(),+ getCategories: vi.fn(),+}))+vi.mock('../src/services/registryClient.js', async importOriginal => ({+ ...(await importOriginal<typeof import('../src/services/registryClient.js')>()),+ searchEntries: registryClient.searchEntries,+ getCategories: registryClient.getCategories,+}))++vi.mock('../src/middleware/rateLimitMiddleware.js', () => ({+ rateLimitMiddleware:+ () => (_req: express.Request, _res: express.Response, next: express.NextFunction) =>+ next(),+}))++describe('app: registry-unavailable forwarding at the global error handler (real middleware)', () => {+ beforeEach(() => {+ vi.clearAllMocks()+ })++ it('forwards a RegistryUnavailableError as 503 { error: registry_unavailable, message }', async () => {+ registryClient.searchEntries.mockRejectedValue(new RegistryUnavailableError())+ registryClient.getCategories.mockRejectedValue(new RegistryUnavailableError())+ const app = createApp(new MockGateway('mcp-server') as never)+ const res = await request(app).get('/api/v1/admin/registry/catalog')+ expect(res.status).toBe(503)+ expect(res.body.error).toBe('registry_unavailable')+ expect(res.body.message).toBe(+ 'The registry is currently unavailable. Check the connection and try again.'+ )+ })++ it('collapses a plain status-less Error to 500 Internal Server Error', async () => {+ registryClient.searchEntries.mockRejectedValue(new Error('boom'))+ registryClient.getCategories.mockRejectedValue(new Error('boom'))+ const app = createApp(new MockGateway('mcp-server') as never)+ const res = await request(app).get('/api/v1/admin/registry/catalog')+ expect(res.status).toBe(500)+ expect(res.body.error).toBe('Internal Server Error')+ })++ it('does NOT forward a 502 with a non-allowlisted code (allowlist enforced) → 500', async () => {+ registryClient.searchEntries.mockRejectedValue(+ Object.assign(new Error('nope'), { status: 502, code: 'other' })+ )+ registryClient.getCategories.mockRejectedValue(+ Object.assign(new Error('nope'), { status: 502, code: 'other' })+ )+ const app = createApp(new MockGateway('mcp-server') as never)+ const res = await request(app).get('/api/v1/admin/registry/catalog')+ expect(res.status).toBe(500)+ expect(res.body.error).toBe('Internal Server Error')+ })+})