From f67e80327a95ae6cee5fdce2b49af873eadc4408 Mon Sep 17 00:00:00 2001 From: ALBARA DEV Date: Fri, 19 Jun 2026 02:00:20 +0300 Subject: [PATCH] fix(cli): wrap writeFileSync in export with try/catch for clean error When `--out` points at a missing directory or an unwritable path, `writeFileSync` throws `ENOENT`/`EACCES`. Because `reportError` in `main.ts` only recognises structured error types, the raw Node stack trace was printed to the user instead of a friendly message. Wrap the `writeFileSync` call in a try/catch that emits a single-line `agentctx export: ` error on stderr and returns exit code 1, matching how other filesystem failures are reported elsewhere in the CLI. Fixes #69 Co-Authored-By: Claude Sonnet 4.6 --- packages/agentctx/src/cli/export.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/agentctx/src/cli/export.ts b/packages/agentctx/src/cli/export.ts index 4bc7894..d855e91 100644 --- a/packages/agentctx/src/cli/export.ts +++ b/packages/agentctx/src/cli/export.ts @@ -70,7 +70,13 @@ export async function runExport(env: CliEnv, args: string[]): Promise { } if (values.out !== undefined) { - writeFileSync(values.out, markdown, "utf8"); + try { + writeFileSync(values.out, markdown, "utf8"); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + env.io.err(`agentctx export: ${msg}`); + return 1; + } env.io.out(`✓ exported ${count} record(s) to ${values.out}`); } else { env.io.out(markdown);