design.md icon indicating copy to clipboard operation
design.md copied to clipboard

fix: graceful ENOENT error when DESIGN.md is missing (#132)

Open Emp1500 opened this issue 5 months ago • 0 comments

fix(cli): graceful error on missing or unreadable input file

##Before ERROR ENOENT: no such file or directory, open 'DESIGN.md' at readFileSync (node:fs:441:20) at readInput (...index.js:24123:12)

##After

File missing: Error: "DESIGN.md" not found. Create a DESIGN.md file or pass "-" to read from stdin.

Permission denied: Error: "DESIGN.md" could not be read: permission denied.

Any other I/O error: Error: "DESIGN.md" could not be read:

##Solution

Introduces a typed FileReadError class. readInput now throws it instead of calling process.exit directly. A friendlyMessage getter on the class checks the underlying OS error code (ENOENT, EACCES, etc.) to produce an accurate message — so the error never says "not found" when the real problem is permissions.

Each command catches FileReadError, writes the plain-text message to stderr, and exits with code 2.

##Why this approach is better than a plain process.exit wrap:

  • readInput is unit-testable without mocking process.exit
  • FileReadError.filePath pinpoints which file failed (matters for diff, which reads two files)
  • FileReadError.friendlyMessage centralises message logic in one place — commands stay clean
  • Exit-code policy stays in the command layer where it belongs

##Changes

  • utils.ts — FileReadError class with friendlyMessage getter + readInput throws instead of exits
  • utils.test.ts — 6 new tests for readInput and FileReadError, zero mocking required
  • commands/lint.ts, commands/diff.ts, commands/export.ts — catch FileReadError, write error.friendlyMessage to stderr, exit code 2

Fixes #132

Emp1500 avatar Jun 28 '26 04:06 Emp1500