Skip to content

Commit 8ebb29a

Browse files
committed
Download response uses shared error parser
1 parent 448ce88 commit 8ebb29a

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

cmd/cmd.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"strings"
78

89
"io"
910

@@ -78,14 +79,22 @@ func validateUserConfig(cfg *viper.Viper) error {
7879
func decodedAPIError(resp *http.Response) error {
7980
var apiError struct {
8081
Error struct {
81-
Type string `json:"type"`
82-
Message string `json:"message"`
82+
Type string `json:"type"`
83+
Message string `json:"message"`
84+
PossibleTrackIDs []string `json:"possible_track_ids"`
8385
} `json:"error,omitempty"`
8486
}
8587
if err := json.NewDecoder(resp.Body).Decode(&apiError); err != nil {
8688
return fmt.Errorf("failed to parse API error response: %s", err)
8789
}
8890
if apiError.Error.Message != "" {
91+
if apiError.Error.Type == "track_ambiguous" {
92+
return fmt.Errorf(
93+
"%s: %s",
94+
apiError.Error.Message,
95+
strings.Join(apiError.Error.PossibleTrackIDs, ", "),
96+
)
97+
}
8998
return fmt.Errorf(apiError.Error.Message)
9099
}
91100
return fmt.Errorf("unexpected API response: %d", resp.StatusCode)

cmd/download.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,7 @@ func newDownload(flags *pflag.FlagSet, usrCfg *viper.Viper) (*download, error) {
204204
defer res.Body.Close()
205205

206206
if err := json.NewDecoder(res.Body).Decode(&d.payload); err != nil {
207-
return nil, fmt.Errorf("unable to parse API response - %s", err)
208-
}
209-
210-
if res.StatusCode == http.StatusUnauthorized {
211-
return nil, fmt.Errorf(
212-
"unauthorized request. Please run the configure command. You can find your API token at %s/my/settings",
213-
config.InferSiteURL(d.apibaseurl),
214-
)
215-
}
216-
if res.StatusCode != http.StatusOK {
217-
switch d.payload.Error.Type {
218-
case "track_ambiguous":
219-
return nil, fmt.Errorf(
220-
"%s: %s",
221-
d.payload.Error.Message,
222-
strings.Join(d.payload.Error.PossibleTrackIDs, ", "),
223-
)
224-
default:
225-
return nil, errors.New(d.payload.Error.Message)
226-
}
207+
return nil, decodedAPIError(res)
227208
}
228209

229210
return d, nil

0 commit comments

Comments
 (0)