From 5d04b98793d3236b1e8608dc7e38d705ba0be685 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 09:45:43 -0700 Subject: [PATCH 1/5] feat(api): exit with an error when the response has "ok": false 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. --- cmd/api/api.go | 17 ++++++ cmd/api/api_test.go | 81 ++++++++++++++++++++++++++++ docs/reference/commands/slack_api.md | 3 ++ 3 files changed, 101 insertions(+) diff --git a/cmd/api/api.go b/cmd/api/api.go index 767be639..65aa1489 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -80,6 +80,9 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command { "Use --no-auth to skip authentication entirely and send the request without", "a token.", "", + "The response body is always printed. The command exits with an error when the", + "HTTP status is outside 2xx or the JSON response has \"ok\": false.", + "", "See all methods at: https://docs.slack.dev/reference/methods", }, "\n"), Example: style.ExampleCommandsf([]style.ExampleCommand{ @@ -259,6 +262,20 @@ func runAPICommand(cmd *cobra.Command, clients *shared.ClientFactory, args []str WithMessage("API request failed with status %d", resp.StatusCode) } + // Slack returns HTTP 200 for most failures, with "ok": false in the body + var result struct { + OK *bool `json:"ok"` + Error string `json:"error"` + } + if json.Unmarshal(resp.Body, &result) == nil && result.OK != nil && !*result.OK { + if result.Error == "" { + return slackerror.New("api_request_failed"). + WithMessage("API request failed with \"ok\": false") + } + return slackerror.New("api_request_failed"). + WithMessage("API request failed with error %q", result.Error) + } + return nil } diff --git a/cmd/api/api_test.go b/cmd/api/api_test.go index c5e9e6c9..2135a039 100644 --- a/cmd/api/api_test.go +++ b/cmd/api/api_test.go @@ -330,6 +330,87 @@ func Test_runAPICommand_NonOKStatus(t *testing.T) { assert.Contains(t, output, `"ok":false`) } +func Test_runAPICommand_OKField(t *testing.T) { + tests := map[string]struct { + flags cmdFlags + body string + expectedError string + expectedOut []string + }{ + "ok false with error returns the error": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":false,"error":"invalid_auth"}`, + expectedError: `API request failed with error "invalid_auth"`, + expectedOut: []string{`{"ok":false,"error":"invalid_auth"}`}, + }, + "ok false without error returns an error": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":false}`, + expectedError: `API request failed with "ok": false`, + expectedOut: []string{`{"ok":false}`}, + }, + "ok false with --include keeps the headers": { + flags: cmdFlags{method: "POST", include: true}, + body: `{"ok":false,"error":"not_authed"}`, + expectedError: `API request failed with error "not_authed"`, + expectedOut: []string{"HTTP 200", `{"ok":false,"error":"not_authed"}`}, + }, + "ok true succeeds": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":true,"user_id":"U123"}`, + expectedOut: []string{`{"ok":true,"user_id":"U123"}`}, + }, + "JSON without ok succeeds": { + flags: cmdFlags{method: "POST"}, + body: `{"error":"not_an_ok_field"}`, + expectedOut: []string{`{"error":"not_an_ok_field"}`}, + }, + "JSON array succeeds": { + flags: cmdFlags{method: "POST"}, + body: `[{"ok":false}]`, + expectedOut: []string{`[{"ok":false}]`}, + }, + "non-JSON body succeeds": { + flags: cmdFlags{method: "POST"}, + body: "ok=false", + expectedOut: []string{"ok=false"}, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, tc.body) + })) + defer server.Close() + + ctx := slackcontext.MockContext(t.Context()) + clientsMock := shared.NewClientsMock() + clientsMock.AddDefaultMocks() + clientsMock.Config.TokenFlag = "xoxb-test-token" + clientsMock.Config.APIHostResolved = server.URL + clients := shared.NewClientFactory(clientsMock.MockClientFactory()) + + cmd := NewCommand(clients) + testutil.MockCmdIO(clients.IO, cmd) + + flags = tc.flags + cmd.SetArgs([]string{"auth.test"}) + err := cmd.ExecuteContext(ctx) + + if tc.expectedError != "" { + assert.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedError) + } else { + assert.NoError(t, err) + } + output := clientsMock.GetStdoutOutput() + for _, expected := range tc.expectedOut { + assert.Contains(t, output, expected) + } + }) + } +} + func Test_runAPICommand_InvalidParam(t *testing.T) { ctx := slackcontext.MockContext(t.Context()) clientsMock := shared.NewClientsMock() diff --git a/docs/reference/commands/slack_api.md b/docs/reference/commands/slack_api.md index 0d3eb13e..39552c05 100644 --- a/docs/reference/commands/slack_api.md +++ b/docs/reference/commands/slack_api.md @@ -31,6 +31,9 @@ If no token is available, the request is sent without authentication. Use --no-auth to skip authentication entirely and send the request without a token. +The response body is always printed. The command exits with an error when the +HTTP status is outside 2xx or the JSON response has "ok": false. + See all methods at: https://docs.slack.dev/reference/methods ``` From 17579bd962d373142909243620590191eae6720e Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 09:53:53 -0700 Subject: [PATCH 2/5] fix(api): match the "ok" key exactly and handle a non-string error 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. --- cmd/api/api.go | 32 +++++++++++++++++++++----------- cmd/api/api_test.go | 17 +++++++++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/cmd/api/api.go b/cmd/api/api.go index 65aa1489..ca6ec696 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -262,23 +262,33 @@ func runAPICommand(cmd *cobra.Command, clients *shared.ClientFactory, args []str WithMessage("API request failed with status %d", resp.StatusCode) } - // Slack returns HTTP 200 for most failures, with "ok": false in the body - var result struct { - OK *bool `json:"ok"` - Error string `json:"error"` - } - if json.Unmarshal(resp.Body, &result) == nil && result.OK != nil && !*result.OK { - if result.Error == "" { - return slackerror.New("api_request_failed"). - WithMessage("API request failed with \"ok\": false") + // 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. + var result map[string]json.RawMessage + if json.Unmarshal(resp.Body, &result) == nil { + var ok bool + if raw, exists := result["ok"]; exists && json.Unmarshal(raw, &ok) == nil && !ok { + return apiResponseError(result["error"]) } - return slackerror.New("api_request_failed"). - WithMessage("API request failed with error %q", result.Error) } return nil } +// apiResponseError returns the error for a response with "ok": false +func apiResponseError(rawError json.RawMessage) error { + var message string + if json.Unmarshal(rawError, &message) != nil { + message = string(bytes.TrimSpace(rawError)) + } + if message == "" || message == "null" { + return slackerror.New("api_request_failed"). + WithMessage("API request failed with \"ok\": false") + } + return slackerror.New("api_request_failed"). + WithMessage("API request failed with error %q", message) +} + // parseParams converts key=value arguments into url.Values func parseParams(params []string) (url.Values, error) { values := url.Values{} diff --git a/cmd/api/api_test.go b/cmd/api/api_test.go index 2135a039..bfdf293d 100644 --- a/cmd/api/api_test.go +++ b/cmd/api/api_test.go @@ -355,6 +355,23 @@ func Test_runAPICommand_OKField(t *testing.T) { expectedError: `API request failed with error "not_authed"`, expectedOut: []string{"HTTP 200", `{"ok":false,"error":"not_authed"}`}, }, + "ok false with a non-string error returns the error": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":false,"error":{"code":"invalid_auth"}}`, + expectedError: `API request failed with error "{\"code\":\"invalid_auth\"}"`, + expectedOut: []string{`{"ok":false,"error":{"code":"invalid_auth"}}`}, + }, + "ok false with a null error returns an error": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":false,"error":null}`, + expectedError: `API request failed with "ok": false`, + expectedOut: []string{`{"ok":false,"error":null}`}, + }, + "uppercase OK key succeeds": { + flags: cmdFlags{method: "POST"}, + body: `{"OK":false}`, + expectedOut: []string{`{"OK":false}`}, + }, "ok true succeeds": { flags: cmdFlags{method: "POST"}, body: `{"ok":true,"user_id":"U123"}`, From 1978a44e937eb0ce8e8efdcee0bc4bdc72acd39d Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 09:55:55 -0700 Subject: [PATCH 3/5] fix(api): treat "ok": null as no ok value JSON null decodes into a bool as false, so "ok": null exited 1. Also asserts a response header in the --include case. --- cmd/api/api.go | 4 ++-- cmd/api/api_test.go | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/api/api.go b/cmd/api/api.go index ca6ec696..24fa43af 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -266,8 +266,8 @@ func runAPICommand(cmd *cobra.Command, clients *shared.ClientFactory, args []str // A map keeps the key match exact, since struct fields match any case. var result map[string]json.RawMessage if json.Unmarshal(resp.Body, &result) == nil { - var ok bool - if raw, exists := result["ok"]; exists && json.Unmarshal(raw, &ok) == nil && !ok { + var ok *bool + if json.Unmarshal(result["ok"], &ok) == nil && ok != nil && !*ok { return apiResponseError(result["error"]) } } diff --git a/cmd/api/api_test.go b/cmd/api/api_test.go index bfdf293d..1fb5ba36 100644 --- a/cmd/api/api_test.go +++ b/cmd/api/api_test.go @@ -353,7 +353,7 @@ func Test_runAPICommand_OKField(t *testing.T) { flags: cmdFlags{method: "POST", include: true}, body: `{"ok":false,"error":"not_authed"}`, expectedError: `API request failed with error "not_authed"`, - expectedOut: []string{"HTTP 200", `{"ok":false,"error":"not_authed"}`}, + expectedOut: []string{"HTTP 200", "X-Custom: test-value", `{"ok":false,"error":"not_authed"}`}, }, "ok false with a non-string error returns the error": { flags: cmdFlags{method: "POST"}, @@ -367,6 +367,11 @@ func Test_runAPICommand_OKField(t *testing.T) { expectedError: `API request failed with "ok": false`, expectedOut: []string{`{"ok":false,"error":null}`}, }, + "ok null succeeds": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":null}`, + expectedOut: []string{`{"ok":null}`}, + }, "uppercase OK key succeeds": { flags: cmdFlags{method: "POST"}, body: `{"OK":false}`, @@ -396,6 +401,7 @@ func Test_runAPICommand_OKField(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Custom", "test-value") fmt.Fprint(w, tc.body) })) defer server.Close() From 6219807b813c429063466bb4f00da0986c84ff98 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 09:57:42 -0700 Subject: [PATCH 4/5] fix(api): show an "error" string of "null" as the error --- cmd/api/api.go | 7 ++++--- cmd/api/api_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/api/api.go b/cmd/api/api.go index 24fa43af..fef53d00 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -278,10 +278,11 @@ func runAPICommand(cmd *cobra.Command, clients *shared.ClientFactory, args []str // apiResponseError returns the error for a response with "ok": false func apiResponseError(rawError json.RawMessage) error { var message string - if json.Unmarshal(rawError, &message) != nil { - message = string(bytes.TrimSpace(rawError)) + rawError = bytes.TrimSpace(rawError) + if len(rawError) > 0 && !bytes.Equal(rawError, []byte("null")) && json.Unmarshal(rawError, &message) != nil { + message = string(rawError) } - if message == "" || message == "null" { + if message == "" { return slackerror.New("api_request_failed"). WithMessage("API request failed with \"ok\": false") } diff --git a/cmd/api/api_test.go b/cmd/api/api_test.go index 1fb5ba36..b40869d1 100644 --- a/cmd/api/api_test.go +++ b/cmd/api/api_test.go @@ -361,6 +361,12 @@ func Test_runAPICommand_OKField(t *testing.T) { expectedError: `API request failed with error "{\"code\":\"invalid_auth\"}"`, expectedOut: []string{`{"ok":false,"error":{"code":"invalid_auth"}}`}, }, + "ok false with a \"null\" string error returns the error": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":false,"error":"null"}`, + expectedError: `API request failed with error "null"`, + expectedOut: []string{`{"ok":false,"error":"null"}`}, + }, "ok false with a null error returns an error": { flags: cmdFlags{method: "POST"}, body: `{"ok":false,"error":null}`, From 6dfa6ab9e38652b51012bccd10136e25f9fb5744 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 10:26:33 -0700 Subject: [PATCH 5/5] refactor(api): return known Slack API errors with their remediation 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. --- cmd/api/api.go | 17 +++++++---------- cmd/api/api_test.go | 20 ++++++++++++++++---- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/cmd/api/api.go b/cmd/api/api.go index fef53d00..d1c5be8b 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -262,32 +262,29 @@ func runAPICommand(cmd *cobra.Command, clients *shared.ClientFactory, args []str WithMessage("API request failed with status %d", resp.StatusCode) } - // 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. 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 { - return apiResponseError(result["error"]) + return apiResponseError(result["error"], method) } } return nil } -// apiResponseError returns the error for a response with "ok": false -func apiResponseError(rawError json.RawMessage) error { - var message string +func apiResponseError(rawError json.RawMessage, method string) error { rawError = bytes.TrimSpace(rawError) - if len(rawError) > 0 && !bytes.Equal(rawError, []byte("null")) && json.Unmarshal(rawError, &message) != nil { - message = string(rawError) + var code string + if json.Unmarshal(rawError, &code) == nil && code != "" { + return slackerror.NewAPIError(code, "", nil, method) } - if message == "" { + if len(rawError) == 0 || bytes.Equal(rawError, []byte("null")) || bytes.Equal(rawError, []byte(`""`)) { return slackerror.New("api_request_failed"). WithMessage("API request failed with \"ok\": false") } return slackerror.New("api_request_failed"). - WithMessage("API request failed with error %q", message) + WithMessage("API request failed with error %s", rawError) } // parseParams converts key=value arguments into url.Values diff --git a/cmd/api/api_test.go b/cmd/api/api_test.go index b40869d1..f13a6a8c 100644 --- a/cmd/api/api_test.go +++ b/cmd/api/api_test.go @@ -340,7 +340,7 @@ func Test_runAPICommand_OKField(t *testing.T) { "ok false with error returns the error": { flags: cmdFlags{method: "POST"}, body: `{"ok":false,"error":"invalid_auth"}`, - expectedError: `API request failed with error "invalid_auth"`, + expectedError: "Your user account authorization isn't valid (invalid_auth)", expectedOut: []string{`{"ok":false,"error":"invalid_auth"}`}, }, "ok false without error returns an error": { @@ -352,19 +352,31 @@ func Test_runAPICommand_OKField(t *testing.T) { "ok false with --include keeps the headers": { flags: cmdFlags{method: "POST", include: true}, body: `{"ok":false,"error":"not_authed"}`, - expectedError: `API request failed with error "not_authed"`, + expectedError: "(not_authed)", expectedOut: []string{"HTTP 200", "X-Custom: test-value", `{"ok":false,"error":"not_authed"}`}, }, + "ok false with an unknown error returns the error code": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":false,"error":"some_new_error"}`, + expectedError: "(some_new_error)", + expectedOut: []string{`{"ok":false,"error":"some_new_error"}`}, + }, + "ok false with an empty error returns an error": { + flags: cmdFlags{method: "POST"}, + body: `{"ok":false,"error":""}`, + expectedError: `API request failed with "ok": false`, + expectedOut: []string{`{"ok":false,"error":""}`}, + }, "ok false with a non-string error returns the error": { flags: cmdFlags{method: "POST"}, body: `{"ok":false,"error":{"code":"invalid_auth"}}`, - expectedError: `API request failed with error "{\"code\":\"invalid_auth\"}"`, + expectedError: `API request failed with error {"code":"invalid_auth"}`, expectedOut: []string{`{"ok":false,"error":{"code":"invalid_auth"}}`}, }, "ok false with a \"null\" string error returns the error": { flags: cmdFlags{method: "POST"}, body: `{"ok":false,"error":"null"}`, - expectedError: `API request failed with error "null"`, + expectedError: "(null)", expectedOut: []string{`{"ok":false,"error":"null"}`}, }, "ok false with a null error returns an error": {