X Tutup
Skip to content

Commit 8f96e40

Browse files
committed
Improve error handling and avoid writing confirmation to stdout
Right now the `gist edit` command doesn't write anything to stdout, so let's keep it that way until we want to intentionally provide some feedback in the terminal.
1 parent 406d7ee commit 8f96e40

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

pkg/cmd/gist/edit/edit.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ func editRun(opts *EditOptions) error {
9090

9191
gist, err := shared.GetGist(client, ghinstance.OverridableDefault(), gistID)
9292
if err != nil {
93+
if errors.Is(err, shared.NotFoundErr) {
94+
return fmt.Errorf("gist not found: %s", gistID)
95+
}
9396
return err
9497
}
9598

@@ -102,27 +105,18 @@ func editRun(opts *EditOptions) error {
102105
return fmt.Errorf("You do not own this gist.")
103106
}
104107

105-
filesToUpdate := map[string]string{}
106-
107-
addFilename := opts.AddFilename
108-
cs := opts.IO.ColorScheme()
109-
110-
if addFilename != "" {
111-
files, err := getFilesToAdd(addFilename)
108+
if opts.AddFilename != "" {
109+
files, err := getFilesToAdd(opts.AddFilename)
112110
if err != nil {
113111
return err
114112
}
115113

116114
gist.Files = files
117-
err = updateGist(apiClient, ghinstance.OverridableDefault(), gist)
118-
if err != nil {
119-
return err
120-
}
121-
122-
fmt.Fprintf(opts.IO.Out, "%s %s added to gist\n", cs.SuccessIconWithColor(cs.Green), filepath.Base(addFilename))
123-
return nil
115+
return updateGist(apiClient, ghinstance.OverridableDefault(), gist)
124116
}
125117

118+
filesToUpdate := map[string]string{}
119+
126120
for {
127121
filename := opts.EditFilename
128122
candidates := []string{}

pkg/cmd/gist/edit/edit_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,12 @@ func Test_editRun(t *testing.T) {
106106
httpStubs func(*httpmock.Registry)
107107
askStubs func(*prompt.AskStubber)
108108
nontty bool
109-
wantErr bool
110-
wantStderr string
109+
wantErr string
111110
wantParams map[string]interface{}
112111
}{
113112
{
114113
name: "no such gist",
115-
wantErr: true,
114+
wantErr: "gist not found: 1234",
116115
},
117116
{
118117
name: "one file",
@@ -195,7 +194,7 @@ func Test_editRun(t *testing.T) {
195194
as.StubOne("unix.md")
196195
as.StubOne("Cancel")
197196
},
198-
wantErr: true,
197+
wantErr: "SilentError",
199198
gist: &shared.Gist{
200199
ID: "1234",
201200
Files: map[string]*shared.GistFile{
@@ -240,8 +239,7 @@ func Test_editRun(t *testing.T) {
240239
},
241240
Owner: &shared.GistOwner{Login: "octocat2"},
242241
},
243-
wantErr: true,
244-
wantStderr: "You do not own this gist.",
242+
wantErr: "You do not own this gist.",
245243
},
246244
{
247245
name: "add file to existing gist",
@@ -299,7 +297,7 @@ func Test_editRun(t *testing.T) {
299297
tt.opts.HttpClient = func() (*http.Client, error) {
300298
return &http.Client{Transport: reg}, nil
301299
}
302-
io, _, _, _ := iostreams.Test()
300+
io, _, stdout, stderr := iostreams.Test()
303301
io.SetStdoutTTY(!tt.nontty)
304302
io.SetStdinTTY(!tt.nontty)
305303
tt.opts.IO = io
@@ -313,11 +311,8 @@ func Test_editRun(t *testing.T) {
313311
t.Run(tt.name, func(t *testing.T) {
314312
err := editRun(tt.opts)
315313
reg.Verify(t)
316-
if tt.wantErr {
317-
assert.Error(t, err)
318-
if tt.wantStderr != "" {
319-
assert.EqualError(t, err, tt.wantStderr)
320-
}
314+
if tt.wantErr != "" {
315+
assert.EqualError(t, err, tt.wantErr)
321316
return
322317
}
323318
assert.NoError(t, err)
@@ -331,6 +326,9 @@ func Test_editRun(t *testing.T) {
331326
}
332327
assert.Equal(t, tt.wantParams, reqBody)
333328
}
329+
330+
assert.Equal(t, "", stdout.String())
331+
assert.Equal(t, "", stderr.String())
334332
})
335333
}
336334
}

pkg/cmd/gist/shared/shared.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shared
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67
"net/url"
@@ -31,13 +32,19 @@ type Gist struct {
3132
Owner *GistOwner `json:"owner,omitempty"`
3233
}
3334

35+
var NotFoundErr = errors.New("not found")
36+
3437
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {
3538
gist := Gist{}
3639
path := fmt.Sprintf("gists/%s", gistID)
3740

3841
apiClient := api.NewClientFromHTTP(client)
3942
err := apiClient.REST(hostname, "GET", path, nil, &gist)
4043
if err != nil {
44+
var httpErr api.HTTPError
45+
if errors.As(err, &httpErr) && httpErr.StatusCode == 404 {
46+
return nil, NotFoundErr
47+
}
4148
return nil, err
4249
}
4350

0 commit comments

Comments
 (0)
X Tutup