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); });