From c4b631c4c4943ac27799fece660dc30d201a8275 Mon Sep 17 00:00:00 2001 From: Mavis Date: Tue, 2 Jun 2026 23:20:20 +0000 Subject: [PATCH] fix: add BUILD_TAG to bridge so we can verify which code is running If you see [v2-debug] in the log, you're on the new code. If you don't, you're still on main and the old error handler is hiding the real error. Also hardened the fatal catch handler to never crash on weird error values. --- apps/tools/ksp-bridge/src/index.ts | 38 ++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/apps/tools/ksp-bridge/src/index.ts b/apps/tools/ksp-bridge/src/index.ts index 557ff6f..86605c0 100644 --- a/apps/tools/ksp-bridge/src/index.ts +++ b/apps/tools/ksp-bridge/src/index.ts @@ -46,8 +46,10 @@ function err(msg: string): void { console.error(`[ksp-bridge] ${msg}`); } +const BUILD_TAG = 'v2-debug'; + async function main(): Promise { - log(`config: api=${API_URL} host=${HOST}:${RPC_PORT} poll=${POLL_MS}ms`); + log(`config: api=${API_URL} host=${HOST}:${RPC_PORT} poll=${POLL_MS}ms} [${BUILD_TAG}]`); // First, try to connect to a real kRPC server. If it works, run // the real extract loop. If it fails, fall back to mock mode. @@ -60,8 +62,28 @@ async function main(): Promise { await adapter.connect(); log(`connected to kRPC at ${HOST}:${RPC_PORT} — running with real KSP state`); } catch (e) { - const msg = e === null ? 'null' : e instanceof Error ? e.message : String(e); - log(`no kRPC server at ${HOST}:${RPC_PORT}: ${msg}`); + // Hardened error formatter: handles null, undefined, Error, + // strings, and arbitrary objects. Never crashes the formatter + // itself, so we always see *something*. + let msg: string; + if (e === null) { + msg = 'null'; + } else if (e === undefined) { + msg = 'undefined'; + } else if (typeof e === 'string') { + msg = e; + } else if (e instanceof Error) { + msg = e.message; + } else if (typeof e === 'object') { + try { + msg = JSON.stringify(e); + } catch { + msg = String(e); + } + } else { + msg = String(e); + } + log(`[${BUILD_TAG}] no kRPC server at ${HOST}:${RPC_PORT}: ${msg}`); log('Falling back to MOCK mode (synthetic state).'); return runMock(); } @@ -165,6 +187,14 @@ function mockState(ut: number): ExtractedState { } main().catch((e) => { - err(`fatal: ${e}`); + let msg: string; + if (e === null) msg = 'null'; + else if (e === undefined) msg = 'undefined'; + else if (e instanceof Error) msg = `${e.message} (stack: ${e.stack ?? 'n/a'})`; + else if (typeof e === 'string') msg = e; + else if (typeof e === 'object') { + try { msg = JSON.stringify(e); } catch { msg = String(e); } + } else msg = String(e); + err(`fatal: ${msg}`); process.exit(1); });