← Back to PRs

#4784: fix(imessage): handle non-JSON output from imsg CLI gracefully

by spiceoogway open 2026-01-30 17:27 View on GitHub →
channel: imessage
Fixes #4707 ## Problem When the CLI encounters errors (e.g., permission denied accessing chat.db), it outputs plain-text error messages to stdout instead of properly formatted JSON-RPC responses. This causes the RPC client to fail parsing and log overwhelming error messages like: ``` msg rpc: failed to parse permissionDenied(path: "/Users/.../chat.db", underlying: authorization denied (code: 23)): Unexpected token 'p', "permission"... is not valid JSON ``` ## Solution This fix improves error handling in the RPC client: - Adds explanatory comments about why non-JSON output can occur - Truncates long lines in error messages (max 200 chars) to prevent log spam - Improves error message clarity: "received non-JSON output" instead of "failed to parse" - Removes unused error detail variable ## Testing - Linter passed with no errors - Change is backward compatible (only affects error handling) <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR updates `IMessageRpcClient`'s stdout line handling to better cope with cases where the `imsg rpc` CLI prints plain-text errors to stdout instead of JSON-RPC. In `src/imessage/client.ts`, parse failures now produce a clearer log message and the logged line is truncated to avoid log spam, while removing an unused error-detail variable. This fits into the existing client flow where stdout is treated as a JSON-RPC stream (`handleLine`) and stderr is already logged line-by-line; the change only affects how stdout parse errors are reported and does not alter request/response handling for valid JSON-RPC frames. <h3>Confidence Score: 4/5</h3> - This PR is safe to merge with low risk; it only changes logging/error reporting around stdout parse failures. - The change is localized to `handleLine`’s JSON.parse error path and does not affect normal JSON-RPC request/response routing. Main remaining concern is reduced diagnosability because the parse error is swallowed, but functionality should be unchanged for valid JSON. - src/imessage/client.ts (only for log/debuggability expectations) <!-- greptile_other_comments_section --> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs