Skip to content

fix(api): exit with an error when the response has "ok": false - #844

Merged
mwbrooks merged 7 commits into
mainfrom
mwbrooks-api-exit-on-ok-false
Nov 24, 2026
Merged

mwbrooks merged 7 commits into
mainfrom
mwbrooks-api-exit-on-ok-false

Conversation

@mwbrooks

@mwbrooks mwbrooks commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Changelog

slack api now exits with an error (exit code 1) when the Slack API response is JSON with "ok": false, and shows Slack's error code with the CLI's usual message and suggestion for it. The response body is still printed to stdout. Scripts that relied on slack api exiting 0 for a failed call will now see a non-zero exit code.

Summary

This pull request makes slack api exit 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 change slack api auth.test --token xoxb-bad printed the error and exited 0.

$ slack api auth.test --token xoxb-bad
{"ok":false,"error":"invalid_auth"}
The following error was returned by the auth.test Slack API method

Your user account authorization isn't valid (invalid_auth)
$ echo $?
1
  • Non-JSON bodies, JSON arrays, and JSON with no ok key behave as before.
  • --include output is unchanged, and the command's long help now describes the exit behaviour.

Testing

  • Run slack api auth.test --token xoxb-bad and confirm the body prints, then the invalid_auth error and its suggestion, with exit code 1.
  • Run slack api api.test --no-auth and confirm {"ok":true,"args":{}} with exit code 0.
  • Run slack api auth.test --token xoxb-bad --include and confirm the status line and headers still print before the body.

Notes

  • The status check runs first, so a non-2xx response keeps its existing status <code> error.
  • The body is printed without a trailing newline, so in a terminal the error's debug log hint starts on the body's last line. This was already true for non-2xx responses.

Requirements

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.
@mwbrooks mwbrooks added this to the Next Release milestone Nov 24, 2026
@mwbrooks mwbrooks added enhancement M-T: A feature request for new functionality changelog Use on updates to be included in the release notes semver:minor Use on pull requests to describe the release version increment labels Nov 24, 2026
@mwbrooks mwbrooks self-assigned this Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.69%. Comparing base (0c385e1) to head (bf7312d).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.
@mwbrooks
mwbrooks marked this pull request as ready for review November 24, 2026 17:16
@mwbrooks
mwbrooks requested review from a team as code owners November 24, 2026 17:16
@mwbrooks
mwbrooks requested review from WilliamBergamin and zimeg and removed request for a team November 24, 2026 17:16

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 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 🙏

Comment thread cmd/api/api.go Outdated
if json.Unmarshal(resp.Body, &result) == nil {
var ok *bool
if json.Unmarshal(result["ok"], &ok) == nil && ok != nil && !*ok {
return apiResponseError(result["error"])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚦 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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/api/api.go Outdated
}

// apiResponseError returns the error for a response with "ok": false
func apiResponseError(rawError json.RawMessage) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪞 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit 6dfa6ab, great suggestion! invalid_auth now shows the usual message and login hint. 🙇

Comment thread cmd/api/api.go Outdated
Comment on lines +265 to +266
// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 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.

Suggested change
// 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in commit 6dfa6ab, thanks!

Comment thread cmd/api/api.go Outdated
return nil
}

// apiResponseError returns the error for a response with "ok": false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 suggestion (non-blocking): Same here, the docstring restates the function name.

Suggested change
// apiResponseError returns the error for a response with "ok": false

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in commit 6dfa6ab, thanks!

Comment thread cmd/api/api_test.go
assert.Contains(t, output, `"ok":false`)
}

func Test_runAPICommand_OKField(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 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.
@mwbrooks

Copy link
Copy Markdown
Member Author

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 WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚦 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 🙏

Comment thread cmd/api/api.go
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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚦 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?

@mwbrooks mwbrooks added semver:patch Use on pull requests to describe the release version increment and removed enhancement M-T: A feature request for new functionality semver:minor Use on pull requests to describe the release version increment labels Nov 24, 2026
@mwbrooks mwbrooks changed the title feat(api): exit with an error when the response has "ok": false fix(api): exit with an error when the response has "ok": false Nov 24, 2026
@mwbrooks mwbrooks added the bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented label Nov 24, 2026
@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin for the review and for talking it through! 🙇

@mwbrooks
mwbrooks merged commit 7dd4824 into main Nov 24, 2026
14 checks passed
@mwbrooks
mwbrooks deleted the mwbrooks-api-exit-on-ok-false branch November 24, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented changelog Use on updates to be included in the release notes semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants