From 9e4ba0249777ac799293d77fbd99541e3381fbce Mon Sep 17 00:00:00 2001 From: Gautam7352 <62495093+Gautam7352@users.noreply.github.com> Date: Wed, 8 Apr 2026 14:15:46 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A7=B9=20Replace=20console.error=20wi?= =?UTF-8?q?th=20custom=20logger=20in=20embeddings=20util?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/utils/embeddings.test.ts | 12 +++--------- server/src/utils/embeddings.ts | 3 ++- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/server/src/utils/embeddings.test.ts b/server/src/utils/embeddings.test.ts index 7bcc7e7..1caf8f5 100644 --- a/server/src/utils/embeddings.test.ts +++ b/server/src/utils/embeddings.test.ts @@ -134,19 +134,13 @@ describe('embeddings util', () => { expect(result).toEqual([]); }); - // Because '[]' won't throw until it gets returned back out, wait... - // Actually `generateEmbeddings` expects response.data to exist and loops over it. - // If `response.data` is empty, `response.data` is `[]`. - // `[...response.data]` is `[]`, mapped is `[]`. - // Returned is `[]`. - // `generateEmbedding` will then do: `const [embedding] = await generateEmbeddings(['valid text']);` - // `embedding` will be `undefined`. - // And throw `AppError(500, 'Embedding generation returned no result.')`. + it('should throw AppError with status 502 on single API error', async () => { + mockCreate.mockRejectedValueOnce(new Error('API failure')); const error = await generateEmbedding('valid text').catch((e) => e); expect(error).toBeInstanceOf(AppError); expect(error.statusCode).toBe(502); - expect(error.message).toContain('Embedding generation failed'); + expect(error.message).toBe('Embedding generation failed: API failure'); }); }); }); diff --git a/server/src/utils/embeddings.ts b/server/src/utils/embeddings.ts index 38caa81..b4e324e 100644 --- a/server/src/utils/embeddings.ts +++ b/server/src/utils/embeddings.ts @@ -7,6 +7,7 @@ import { getOpenRouterClient } from '../lib/openrouter.js'; import { AppError } from './AppError.js'; +import { logger } from './logger.js'; /** Embedding model — must be hosted on OpenRouter or a compatible endpoint */ const EMBEDDING_MODEL = 'openai/text-embedding-3-small'; @@ -43,7 +44,7 @@ export async function generateEmbeddings(texts: string[]): Promise { err instanceof Error ? err.message : 'Unknown error generating embeddings'; - console.error('[Embeddings] API call failed:', message); + logger.error(`[Embeddings] API call failed: ${message}`); throw new AppError(502, `Embedding generation failed: ${message}`); } } From cdcfc45a1d07fd4b6f2e68c3aac6206a97a912b5 Mon Sep 17 00:00:00 2001 From: Gautam7352 <62495093+Gautam7352@users.noreply.github.com> Date: Wed, 8 Apr 2026 14:29:55 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A7=B9=20Fix=20TS=20build=20error=20f?= =?UTF-8?q?rom=20embeddings=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/utils/embeddings.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/utils/embeddings.ts b/server/src/utils/embeddings.ts index b4e324e..8971802 100644 --- a/server/src/utils/embeddings.ts +++ b/server/src/utils/embeddings.ts @@ -53,8 +53,9 @@ export async function generateEmbeddings(texts: string[]): Promise { */ export async function generateEmbedding(text: string): Promise { const result = await generateEmbeddings([text]); - if (result.length === 0) { + const embedding = result[0]; + if (!embedding) { throw new AppError(500, 'Embedding generation returned no result.'); } - return result[0]; + return embedding; } From 0e903b45fb9e3a2ef662d34b48df41b025f1177f Mon Sep 17 00:00:00 2001 From: Gautam7352 <62495093+Gautam7352@users.noreply.github.com> Date: Wed, 8 Apr 2026 15:13:51 +0000 Subject: [PATCH 3/3] Fix CORS configuration to allow missing origins for health checks --- server/src/index.test.ts | 8 +++----- server/src/index.ts | 8 ++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/server/src/index.test.ts b/server/src/index.test.ts index 0db2ed0..75b2b4b 100644 --- a/server/src/index.test.ts +++ b/server/src/index.test.ts @@ -17,17 +17,15 @@ describe('CORS configuration', () => { expect(res.status).not.toBe(500); }); - it('rejects missing origin in production mode', async () => { + it('allows missing origin in production mode (for health checks etc)', async () => { env.NODE_ENV = 'production'; const originalWarn = console.warn; console.warn = vi.fn(); const res = await request(app).get('/health'); - // The error handler in index.ts catches the Error thrown by CORS - // and returns a 500 status with a generic "Internal server error" message. - expect(res.status).toBe(500); - expect(res.body.message).toBe('Internal server error'); + // It should not throw a CORS error + expect(res.status).not.toBe(500); console.warn = originalWarn; }); diff --git a/server/src/index.ts b/server/src/index.ts index 642b2cd..2e0e26d 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -29,13 +29,9 @@ const allowedOrigins = env.ALLOWED_ORIGINS.split(',') app.use( cors({ origin: (origin, callback) => { - // allow server-to-server / curl requests in development, reject in production + // allow server-to-server / curl requests and health checks if (!origin) { - if (env.NODE_ENV !== 'production') { - return callback(null, true); - } - console.warn(`[CORS] Rejected Missing Origin in production`); - return callback(new Error(`CORS: missing origin not allowed`)); + return callback(null, true); } if (allowedOrigins.includes(origin)) return callback(null, true);