Skip to content

Commit 2a86fe3

Browse files
authored
Handle receiving HTTP error codes (non-200) when downloading files. (#1246)
Related to [this forum thread](https://forum.exercism.org/t/cli-silently-creates-0-byte-files-when-file-downloads-hit-http-429-rate-limit/75208/).
1 parent a6e2c53 commit 2a86fe3

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

cmd/download.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
8080
return err
8181
}
8282

83+
var successes int
8384
for _, sf := range download.payload.files() {
8485
url, err := sf.url()
8586
if err != nil {
@@ -98,8 +99,10 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
9899
defer res.Body.Close()
99100

100101
if res.StatusCode != http.StatusOK {
101-
// TODO: deal with it
102-
continue
102+
if successes > 0 {
103+
fmt.Fprintf(Err, "Downloaded %d/%d files\n", successes, len(download.payload.files()))
104+
}
105+
return fmt.Errorf("received HTTP/%d when fetching %#v", res.StatusCode, url)
103106
}
104107

105108
path := sf.relativePath()
@@ -117,6 +120,7 @@ func runDownload(cfg config.Config, flags *pflag.FlagSet, args []string) error {
117120
if err != nil {
118121
return err
119122
}
123+
successes++
120124
}
121125
fmt.Fprintf(Err, "\nDownloaded to\n")
122126
fmt.Fprintf(Out, "%s\n", metadata.Dir)

0 commit comments

Comments
 (0)