Skip to content

Commit

Permalink
add readRequestPartResponses error returns
Browse files Browse the repository at this point in the history
  • Loading branch information
mh0lt committed May 6, 2024
1 parent 1127c42 commit 59a2282
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions webseed/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (me ErrBadResponse) Error() string {
return me.Msg
}

func recvPartResult(ctx context.Context, buf io.Writer, part requestPart, resp *http.Response) error {
func recvPartResult(ctx context.Context, buf *bytes.Buffer, part requestPart, resp *http.Response) error {
defer resp.Body.Close()
var body io.Reader = resp.Body
if part.responseBodyWrapper != nil {
Expand All @@ -133,6 +133,7 @@ func recvPartResult(ctx context.Context, buf io.Writer, part requestPart, resp *
if err != nil {
return err
}

if copied != part.e.Length {
return fmt.Errorf("got %v bytes, expected %v", copied, part.e.Length)
}
Expand Down Expand Up @@ -173,19 +174,18 @@ func recvPartResult(ctx context.Context, buf io.Writer, part requestPart, resp *

var ErrTooFast = errors.New("making requests too fast")

func readRequestPartResponses(ctx context.Context, parts []requestPart) (_ []byte, err error) {
func readRequestPartResponses(ctx context.Context, parts []requestPart) ([]byte, error) {
var buf bytes.Buffer
for _, part := range parts {
result, err := part.do()

if err == nil {
err = recvPartResult(ctx, &buf, part, result)
}

if err != nil {
err = fmt.Errorf("reading %q at %q: %w", part.req.URL, part.req.Header.Get("Range"), err)
break
for _, part := range parts {
if result, err := part.do(); err != nil {
return nil, err
} else {
if err = recvPartResult(ctx, &buf, part, result); err != nil {
return nil, fmt.Errorf("reading %q at %q: %w", part.req.URL, part.req.Header.Get("Range"), err)
}
}
}
return buf.Bytes(), err
}

return buf.Bytes(), nil
}

0 comments on commit 59a2282

Please sign in to comment.