fix(api): teach instead of leaking SyntaxError on malformed --data#8333
fix(api): teach instead of leaking SyntaxError on malformed --data#8333DavidWells wants to merge 1 commit into
Conversation
'netlify api getSite --data site_id=123' leaked a raw SyntaxError. The error now names the flag, echoes the bad input, and shows a correct JSON example. 'Missing required path variable' errors list the method's required path parameters.
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe Estimated code review effort: 2 (Simple) | ~12 minutes Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant apiCommand
participant JSONParse
participant NetlifyAPI
User->>apiCommand: run(apiMethodName, options.data)
apiCommand->>JSONParse: JSON.parse(options.data)
alt parse fails
JSONParse-->>apiCommand: throws SyntaxError
apiCommand-->>User: throw invalid --data JSON error
else parse succeeds
JSONParse-->>apiCommand: payload
apiCommand->>NetlifyAPI: call apiMethodName(payload)
alt Missing required path variable
NetlifyAPI-->>apiCommand: throws Missing required path variable error
apiCommand-->>User: throw --data path variable error with example
else other error
NetlifyAPI-->>apiCommand: throws error
apiCommand-->>User: logAndThrowError(error)
end
end
Related issues: None specified in the provided context. Related PRs: None specified in the provided context. Suggested labels: cli-api, tests Suggested reviewers: None specified in the provided context. 🐰 A rabbit typed a payload with care, 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/unit/commands/api/api.test.ts (1)
59-65: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueTest relies on real OpenAPI
methodsdata rather than a mock.This test depends on the actual
getSiteoperation definition inmethodscontainingsite_idunderparameters.path, sincemethodsisn't mocked. This makes the test implicitly coupled to the real OpenAPI spec content — if the spec changes ordering/naming, this test could fail for reasons unrelated to the change under test.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/commands/api/api.test.ts` around lines 59 - 65, The test is implicitly coupled to the real OpenAPI spec because it depends on getSite and methods containing site_id in parameters.path. Update the test setup to mock the relevant operation metadata (or the methods lookup) so captureError exercises the missing-path-variable behavior without relying on the live spec content. Keep the assertions focused on the error formatting from captureError and the getSite path-variable handling, not on the actual OpenAPI definition.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/unit/commands/api/api.test.ts`:
- Around line 59-65: The test is implicitly coupled to the real OpenAPI spec
because it depends on getSite and methods containing site_id in parameters.path.
Update the test setup to mock the relevant operation metadata (or the methods
lookup) so captureError exercises the missing-path-variable behavior without
relying on the live spec content. Keep the assertions focused on the error
formatting from captureError and the getSite path-variable handling, not on the
actual OpenAPI definition.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 09e8d67a-3cfd-489d-8b8f-09378912381b
📒 Files selected for processing (2)
src/commands/api/api.tstests/unit/commands/api/api.test.ts
🔗 Linked repositories identified
CodeRabbit considers these linked repositories for cross-repo context during reviews:
netlify/blueprints(manual)
Split from #8300 (5 of 9).
What
netlify api getSite --data 'site_id=123'used to leak a raw parser error:Now:
"Missing required path variable" API errors also now list the method's required path parameters.
Why it helps agents
We found a real agent session with 18 consecutive failures on exactly this key=value vs JSON confusion. An error that names the flag, echoes the input, and shows a working example turns that into one failure and one self-correction.
Testing