Skip to content

fix(cli): handle writeFileSync errors in export --out with a clean message#70

Open
Open2Loop wants to merge 1 commit into
agentctxhq:mainfrom
Open2Loop:fix/export-out-error-handling
Open

fix(cli): handle writeFileSync errors in export --out with a clean message#70
Open2Loop wants to merge 1 commit into
agentctxhq:mainfrom
Open2Loop:fix/export-out-error-handling

Conversation

@Open2Loop

Copy link
Copy Markdown

Summary

Fixes #69.

agentctx export --out <path> called writeFileSync with no error handling. When the target directory doesn't exist (or the path isn't writable), the raw Node stack trace was printed instead of a clean error message, because reportError in main.ts only recognises structured error types.

Fix: wrap writeFileSync in a try/catch that emits a single-line agentctx export: <message> error on stderr and returns exit code 1 — matching how other filesystem failures are handled in the CLI.

Change

// Before
writeFileSync(values.out, markdown, "utf8");
env.io.out(`✓ exported ${count} record(s) to ${values.out}`);

// After
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}`);

🤖 Generated with Claude Code

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: <message>` error on stderr and returns exit code 1,
matching how other filesystem failures are reported elsewhere in the CLI.

Fixes agentctxhq#69

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Deepam02

Copy link
Copy Markdown
Contributor

@Open2Loop please add the tests as required by the issue description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agentctx export --out <path> crashes with a raw stack trace when the target directory doesn't exist (or the write fails)

2 participants