1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Fix client request error handling

Signed-off-by: Kostadin Plachkov <k.n.plachkov@gmail.com>
This commit is contained in:
Kostadin Plachkov 2020-12-11 00:13:37 +01:00
parent af34b94a78
commit cd40eb89ae
2 changed files with 50 additions and 3 deletions

View file

@ -110,11 +110,16 @@ func (cli *Client) sendRequest(ctx context.Context, method, path string, query u
if err != nil {
return serverResponse{}, err
}
resp, err := cli.doRequest(ctx, req)
if err != nil {
return resp, errdefs.FromStatusCode(err, resp.statusCode)
switch {
case errors.Is(err, context.Canceled):
return serverResponse{}, errdefs.Cancelled(err)
case errors.Is(err, context.DeadlineExceeded):
return serverResponse{}, errdefs.Deadline(err)
case err == nil:
err = cli.checkResponseErr(resp)
}
err = cli.checkResponseErr(resp)
return resp, errdefs.FromStatusCode(err, resp.statusCode)
}

View file

@ -3,12 +3,14 @@ package client // import "github.com/docker/docker/client"
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/errdefs"
@ -105,3 +107,43 @@ func TestInfiniteError(t *testing.T) {
_, err := client.Ping(context.Background())
assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
}
func TestCanceledContext(t *testing.T) {
testURL := "/test"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Context().Err(), context.Canceled)
return &http.Response{}, context.Canceled
}),
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
assert.Equal(t, true, errdefs.IsCancelled(err))
assert.Equal(t, true, errors.Is(err, context.Canceled))
}
func TestDeadlineExceededContext(t *testing.T) {
testURL := "/test"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Context().Err(), context.DeadlineExceeded)
return &http.Response{}, context.DeadlineExceeded
}),
}
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
defer cancel()
<-ctx.Done()
_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
assert.Equal(t, true, errdefs.IsDeadline(err))
assert.Equal(t, true, errors.Is(err, context.DeadlineExceeded))
}