Skip to content

Commit b6cb655

Browse files
committed
[rget]fix error handling
1 parent 0f1c350 commit b6cb655

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

kadai3/imura81gt/rget/cmd/rget/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ func main() {
2727

2828
option.URL = urls[0]
2929
fmt.Println(option)
30-
rget.Run(option)
30+
err := rget.Run(option)
31+
if err != nil {
32+
fmt.Fprintf(os.Stderr, "err: %s", err)
33+
os.Exit(1)
34+
}
3135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestMain(t *testing.T) {}

kadai3/imura81gt/rget/rget.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,33 @@ func (u *Unit) Write(data []byte) (int, error) {
3838

3939
type Units []Unit
4040

41-
func Run(option Option) {
41+
func Run(option Option) error {
4242
fmt.Printf("%+v\n", option)
4343
err := option.checkingHeaders()
4444
if err != nil {
45-
fmt.Errorf("%s", err)
45+
return fmt.Errorf("%s", err)
4646
}
4747

4848
option.divide()
4949

5050
tmpDir, err := ioutil.TempDir("", "rget")
5151
if err != nil {
52-
fmt.Errorf("%s", err)
52+
return fmt.Errorf("%s", err)
5353
}
5454
defer os.RemoveAll(tmpDir)
5555
fmt.Println(tmpDir)
5656

5757
err = option.parallelDownload(tmpDir)
5858
if err != nil {
59-
fmt.Errorf("%s", err)
59+
return fmt.Errorf("%s", err)
6060
}
6161

6262
err = option.combine(tmpDir)
6363
if err != nil {
64-
fmt.Errorf("%s", err)
64+
return fmt.Errorf("%s", err)
6565
}
6666

67+
return nil
6768
}
6869

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

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

0 commit comments

Comments
 (0)