Skip to content

fix: handle unhandled exceptions so --json always emits valid JSON - #33

Merged
abruption merged 1 commit into
mainfrom
fix/25-unhandled-exceptions-json
Sep 8, 2026
Merged

abruption merged 1 commit into
mainfrom
fix/25-unhandled-exceptions-json

Conversation

@abruption

Copy link
Copy Markdown
Owner

Problem

Three exception paths break the --json contract — callers get a raw traceback instead of parseable JSON:

  1. discover() raises PermissionError when ~/.claude/sessions/ is unreadable
  2. read_message() calls sys.stdin.read() on a TTY when no message is given, hanging forever; binary stdin raises UnicodeDecodeError
  3. Any other uncaught exception produces a traceback instead of {"ok": false, ...}

Fix

  • discover(): catch PermissionError on glob() and return an empty list
  • read_message(): detect isatty() and error immediately; catch UnicodeDecodeError on stdin.read()
  • main(): add except Exception catch-all that formats as JSON when --json is set

Tests

  • test_main_catches_unexpected_exception_json: RuntimeError → JSON output
  • test_main_catches_unexpected_exception_human: RuntimeError → stderr
  • test_discover_survives_permission_error: PermissionError → empty list
  • test_rejects_tty_stdin: TTY → CcPeerError
  • test_catches_binary_stdin: UnicodeDecodeError → CcPeerError

Closes #25

Three paths broke the --json contract:

1. discover() raised PermissionError when the sessions directory was
   unreadable — now catches it and returns an empty list.

2. read_message() called sys.stdin.read() on a TTY when no message was
   given, hanging forever — now detects isatty() and errors immediately.
   Binary stdin causing UnicodeDecodeError is also caught.

3. Any other uncaught exception produced a raw traceback instead of
   JSON — main() now has a catch-all that formats it as
   {"ok": false, "error": "..."}.

Closes #25
@abruption
abruption merged commit 0d9e433 into main Sep 8, 2026
6 checks passed
@abruption
abruption deleted the fix/25-unhandled-exceptions-json branch September 8, 2026 00:49
@abruption abruption mentioned this pull request Sep 8, 2026
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.

Unhandled exceptions break the --json contract

1 participant