Skip to content

Commit

Permalink
[rget]fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
imura81gt committed Dec 22, 2019
1 parent 0f1c350 commit b6cb655
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion kadai3/imura81gt/rget/cmd/rget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ func main() {

option.URL = urls[0]
fmt.Println(option)
rget.Run(option)
err := rget.Run(option)
if err != nil {
fmt.Fprintf(os.Stderr, "err: %s", err)
os.Exit(1)
}
}
5 changes: 5 additions & 0 deletions kadai3/imura81gt/rget/cmd/rget/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import "testing"

func TestMain(t *testing.T) {}
13 changes: 8 additions & 5 deletions kadai3/imura81gt/rget/rget.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,33 @@ func (u *Unit) Write(data []byte) (int, error) {

type Units []Unit

func Run(option Option) {
func Run(option Option) error {
fmt.Printf("%+v\n", option)
err := option.checkingHeaders()
if err != nil {
fmt.Errorf("%s", err)
return fmt.Errorf("%s", err)
}

option.divide()

tmpDir, err := ioutil.TempDir("", "rget")
if err != nil {
fmt.Errorf("%s", err)
return fmt.Errorf("%s", err)
}
defer os.RemoveAll(tmpDir)
fmt.Println(tmpDir)

err = option.parallelDownload(tmpDir)
if err != nil {
fmt.Errorf("%s", err)
return fmt.Errorf("%s", err)
}

err = option.combine(tmpDir)
if err != nil {
fmt.Errorf("%s", err)
return fmt.Errorf("%s", err)
}

return nil
}

func (o *Option) checkingHeaders() error {
Expand Down Expand Up @@ -173,6 +174,8 @@ func (o *Option) downloadWithContext(
req.Header.Set("Range", byteRange)

client := http.DefaultClient
// TODO: should check resp.StatusCode.
// client.Do cannot seems to return the err when statusCode is 50x etc.
resp, err := client.Do(req)
if err != nil {
fmt.Printf("client err: %s", err)
Expand Down

0 comments on commit b6cb655

Please sign in to comment.