Repository navigation
fix(api): exit with an error when the response has "ok": false - #844
Conversation
Slack returns HTTP 200 with {"ok":false,"error":"..."} for most failures,
so `slack api` exited 0 on a failed call. The body is still printed, then
the command returns an error naming the Slack error value.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #844 +/- ##
=======================================
Coverage 80.68% 80.69%
=======================================
Files 247 247
Lines 18824 18841 +17
=======================================
+ Hits 15189 15203 +14
- Misses 3635 3638 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A non-string "error" made json.Unmarshal fail, so the command exited 0 on "ok": false. Struct fields also matched "OK" in any case. Decode into a map so only the exact "ok" key counts and any "error" value is shown.
JSON null decodes into a bool as false, so "ok": null exited 1. Also asserts a response header in the --include case.
WilliamBergamin
left a comment
There was a problem hiding this comment.
🧪 Thanks @mwbrooks! A non-zero exit on ok: false is a real win for scripts, and the edge-case table makes the check easy to trust.
One blocking comment on the exit code change for existing scripts, the rest are optional 🙏
| if json.Unmarshal(resp.Body, &result) == nil { | ||
| var ok *bool | ||
| if json.Unmarshal(result["ok"], &ok) == nil && ok != nil && !*ok { | ||
| return apiResponseError(result["error"]) |
There was a problem hiding this comment.
🚦 issue (blocking): slack api has shipped since v4.10 and exits 0 on an HTTP 200 ok: false, so a script under set -e that reads .error now stops. Could this be opt-in, like a --fail flag, or wait for a major release?
There was a problem hiding this comment.
note: Thanks for raising this! I'd like to keep it on by default, since a failed call exiting 0 is the bug a developer hit. The semver:minor label and Changelog note call out the change for scripts that read .error.
| } | ||
|
|
||
| // apiResponseError returns the error for a response with "ok": false | ||
| func apiResponseError(rawError json.RawMessage) error { |
There was a problem hiding this comment.
🪞 suggestion (non-blocking): Could a string error go through slackerror.NewAPIError(message, "", nil, method) like the rest of the CLI? Known codes like invalid_auth would get their remediation text, with this fallback kept for a missing or non-string error.
There was a problem hiding this comment.
Updated in commit 6dfa6ab, great suggestion! invalid_auth now shows the usual message and login hint. 🙇
| // Slack returns HTTP 200 for most failures, with "ok": false in the body. | ||
| // A map keeps the key match exact, since struct fields match any case. |
There was a problem hiding this comment.
🪓 suggestion (non-blocking): Could these comments go? The uppercase OK key succeeds test already pins the exact key match and fails if the map becomes a struct.
| // Slack returns HTTP 200 for most failures, with "ok": false in the body. | |
| // A map keeps the key match exact, since struct fields match any case. |
| return nil | ||
| } | ||
|
|
||
| // apiResponseError returns the error for a response with "ok": false |
There was a problem hiding this comment.
🪓 suggestion (non-blocking): Same here, the docstring restates the function name.
| // apiResponseError returns the error for a response with "ok": false |
| assert.Contains(t, output, `"ok":false`) | ||
| } | ||
|
|
||
| func Test_runAPICommand_OKField(t *testing.T) { |
There was a problem hiding this comment.
💯 praise: The table covers the tricky shapes (null ok, uppercase OK, arrays, non-JSON), so the check can't widen by accident.
A string "error" now goes through slackerror.NewAPIError, so known codes like invalid_auth show the same message and suggestion as the rest of the CLI. Also drops two comments the tests already cover.
|
Thanks @WilliamBergamin for the thoughtful review! 🙇 The three suggestions are in 6dfa6ab and I replied on the exit code. Would you mind a refresh review when you have a moment? |
WilliamBergamin
left a comment
There was a problem hiding this comment.
🚦 Thanks @mwbrooks! Routing the code through NewAPIError reads great, invalid_auth now gets the usual message and login hint.
The exit code is still the one blocker: the semver:minor label tells folks about it, but existing scripts still break 🙏
| var result map[string]json.RawMessage | ||
| if json.Unmarshal(resp.Body, &result) == nil { | ||
| var ok *bool | ||
| if json.Unmarshal(result["ok"], &ok) == nil && ok != nil && !*ok { |
There was a problem hiding this comment.
🚦 issue (blocking): slack api has exited 0 on an HTTP 200 ok: false since v4.1.0, so a set -e script that reads .error stops after upgrading. Could this be opt-in, like curl's --fail-with-body, or wait for the next major?
|
Thanks @WilliamBergamin for the review and for talking it through! 🙇 |
Changelog
slack apinow exits with an error (exit code 1) when the Slack API response is JSON with"ok": false, and shows Slack'serrorcode with the CLI's usual message and suggestion for it. The response body is still printed to stdout. Scripts that relied onslack apiexiting 0 for a failed call will now see a non-zero exit code.Summary
This pull request makes
slack apiexit 1 when a Slack API call fails, so developers and scripts can tell from the exit code that it failed. Slack returns HTTP 200 with{"ok":false,"error":"..."}for most failures, so before this changeslack api auth.test --token xoxb-badprinted the error and exited 0.okkey behave as before.--includeoutput is unchanged, and the command's long help now describes the exit behaviour.Testing
slack api auth.test --token xoxb-badand confirm the body prints, then theinvalid_autherror and its suggestion, with exit code 1.slack api api.test --no-authand confirm{"ok":true,"args":{}}with exit code 0.slack api auth.test --token xoxb-bad --includeand confirm the status line and headers still print before the body.Notes
status <code>error.Requirements