Repository navigation
fix(api): exit with an error when the response has "ok": false #844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5d04b98
feat(api): exit with an error when the response has "ok": false
mwbrooks 17579bd
fix(api): match the "ok" key exactly and handle a non-string error
mwbrooks 1978a44
fix(api): treat "ok": null as no ok value
mwbrooks 6219807
fix(api): show an "error" string of "null" as the error
mwbrooks 6dfa6ab
refactor(api): return known Slack API errors with their remediation
mwbrooks 5a21ec4
Merge branch 'main' into mwbrooks-api-exit-on-ok-false
mwbrooks bf7312d
Merge branch 'main' into mwbrooks-api-exit-on-ok-false
mwbrooks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -330,6 +330,128 @@ func Test_runAPICommand_NonOKStatus(t *testing.T) { | |
| assert.Contains(t, output, `"ok":false`) | ||
| } | ||
|
|
||
| func Test_runAPICommand_OKField(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 praise: The table covers the tricky shapes (null |
||
| 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: "Your user account authorization isn't valid (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: "(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"}`, | ||
| 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: "(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}`, | ||
| 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}`, | ||
| expectedOut: []string{`{"OK":false}`}, | ||
| }, | ||
| "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) { | ||
| w.Header().Set("X-Custom", "test-value") | ||
| 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() | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚦 issue (blocking):
slack apihas exited 0 on an HTTP 200ok: falsesince v4.1.0, so aset -escript that reads.errorstops after upgrading. Could this be opt-in, like curl's--fail-with-body, or wait for the next major?