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

Merge pull request #41778 from kplachkov/feature/fix_err_canceled

Fix converting status code to error canceled
This commit is contained in:
Akihiro Suda 2021-06-17 17:26:14 +09:00 committed by GitHub
commit b9ad7b96bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 { if err != nil {
return serverResponse{}, err return serverResponse{}, err
} }
resp, err := cli.doRequest(ctx, req) resp, err := cli.doRequest(ctx, req)
if err != nil { switch {
return resp, errdefs.FromStatusCode(err, resp.statusCode) 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) return resp, errdefs.FromStatusCode(err, resp.statusCode)
} }

View file

@ -3,12 +3,14 @@ package client // import "github.com/docker/docker/client"
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
"time"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/errdefs" "github.com/docker/docker/errdefs"
@ -105,3 +107,43 @@ func TestInfiniteError(t *testing.T) {
_, err := client.Ping(context.Background()) _, err := client.Ping(context.Background())
assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error")) 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))
}