Skip to content

io.sh: API errors use the CLI's error JSON when jq is present - #32

Merged
makriman merged 2 commits into
mainfrom
fix/29-io-sh-api-error-shape
Dec 5, 2026
Merged

makriman merged 2 commits into
mainfrom
fix/29-io-sh-api-error-shape

Conversation

@makriman

@makriman makriman commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Problem

With jq installed, io.sh promises the same JSON as the CLI. Errors that come back from the API do not get it. io brief 99999999999 exits 4 (correct) but prints the API's own body on stderr:

{"code":"io_not_found","message":"No such article.","data":{"status":404}}

The CLI prints {"ok":false,"error":"io_not_found","message":"No such article.","status":404}. io.sh's own client-side errors already use the CLI shape, so a script reading .ok or .error missed only the errors that came from the API.

Root cause

call() in io.sh mapped the HTTP status to an exit code and then printed $out to stderr unchanged. Nothing reshaped the error path. The success path was already reshaped with jq.

Change

With jq, call() builds the same object as api() in bin/indianopinion.mjs:

  • error: the body's error, else its code, else http_<status>
  • message: the body's message, else HTTP <status>, with the CLI's own text for rest_no_route
  • status: the HTTP status
  • field, allowed, attempts_left, suggestion, options, scope and retry_after, when the body has them

A body that is not JSON, for example an HTML error page, gives http_<status> as in the CLI. If jq itself fails, the raw body is printed, as before. Without jq nothing changes: the README says that without jq io.sh prints the API's JSON unchanged, and that stays true. README and the io.sh header now say that API errors are reshaped with jq. CHANGELOG.md has an Unreleased entry.

Tests

test/api-error-shape.test.mjs (in npm test) runs the CLI and io.sh side by side against the REST stub and requires the same exit code and the same stderr object for eight kinds of API error: the WordPress-style 404 from the issue, a 400 with attempts_left, a 400 with field, a 429 with retry_after and scope, a 409 ambiguous with options, a refused token (401), an agent API 404, rest_no_route, and an HTML 404. A third test pins the no-jq behaviour (body unchanged). Before the fix the first two tests fail.

Commands run on this branch:

  • npm test (Node 22.23.4, jq 1.7, ../mcp installed): 35 tests, 35 pass, 0 skipped.
  • npm pack --dry-run: the same 6 files.
  • Not run: shellcheck (not installed here), Node 20 and 24, and anything against the live API.

For the reviewer

  • The error bodies in the test are the stub's (test/stub-rest.mjs), which follow the agent API contract and WordPress's REST error format. I did not re-check the live API's error shapes for this change.
  • The CLI reads Retry-After from the response header first; io.sh does not read headers, so it uses the body's retry_after only. The stub sends both, as the API contract does. If the live API ever sends the header without the body field, io.sh will leave retry_after out.
  • The CLI also treats a 2xx answer with "ok": false as an error; io.sh does not. I left that alone because I have no evidence the API sends such answers.

Fixes #29

)

call() printed the API's error body as it came, so a WordPress error
such as {code, message, data: {status}} reached stderr in that shape
while io.sh's own errors and the CLI's used {ok: false, error, message}.
With jq, call() now builds the same object as the CLI's api(): error
from .error or .code (http_<status> for a body that is not JSON),
message, status, and the extra fields the CLI copies. Without jq the
body is still passed through unchanged, as documented.

Test: test/api-error-shape.test.mjs compares io.sh and the CLI on eight
kinds of API error from the REST stub.
@makriman

makriman commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Review by Grok Bot (Indian Opinion QA agent)

Tested at head 55ccf86. Verdict: works.

Fixes: #29. With jq, API errors from io.sh now come out in the CLI's {ok:false, error, message, status} shape on stderr.

What I checked

  • npm test: this head 35 tests, 34 pass, 0 fail, 1 skipped (the MCP bridge test, which skips on main too). Main: 32 tests, 31 pass.
  • Failing first: I copied test/api-error-shape.test.mjs onto main. 2 of 3 fail.
  • Direct repro. A fake curl on PATH returned a fixed body and status, and I ran io.sh brief 99999999999:
Body / status main stderr this head stderr exit (both)
WP {"code":"io_not_found",...,"data":{"status":404}} / 404 the raw body {"ok":false,"error":"io_not_found","message":"No such article.","status":404} 4
<html>404</html> / 404 raw HTML {"ok":false,"error":"http_404","message":"HTTP 404","status":404} 4
agent API 429 with retry_after:"30", scope the raw body same fields plus status:429, retry_after as the number 30 5
{"code":"rest_no_route"} / 404 the raw body the CLI's "not answering yet" message 4
empty body / 500 empty line {"ok":false,"error":"http_500","message":"HTTP 500","status":500} 7
  • With IO_SH_NO_JQ=1 the body is printed unchanged, as the README says.
  • shellcheck io.sh: one extra SC2016 note compared with main (6 against 5). It points at the single-quoted jq program JQ_APIERR, where $st, $d and $c are jq variables. It's a false positive.

Not checked

  • I did not compare the CLI and io.sh side by side myself. The new test does that for eight cases, and it passes.

Review

Nothing blocking. Defining JQ_APIERR below call() is fine, because it is assigned at top level before any command dispatch runs. A # shellcheck disable=SC2016 on that line would keep shellcheck quiet when CI adds it.

Automated checks (this head vs current main)

Command This PR main
npm ci exit 0 exit 0
npm test exit 0: ℹ tests 35; ℹ pass 34; ℹ fail 0 exit 0: ℹ tests 32; ℹ pass 31; ℹ fail 0
npm audit --omit=dev found 0 vulnerabilities found 0 vulnerabilities

I'm Grok Bot, the automated QA agent for Indian Opinion. This is a comment-only review. I don't approve, request changes or merge. Local stack: PHP 8.4.28, Composer 2.11.3, MariaDB 11.8.9, Node 24.22.1, WordPress 7.1.5.

@makriman
makriman marked this pull request as ready for review December 5, 2026 20:05
@makriman
makriman merged commit 10c910a into main Dec 5, 2026
4 checks passed
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.

io.sh: API-originated errors keep the raw WordPress shape instead of the CLI normalized error JSON

1 participant