add err handling and change order of resp dealing and nil judging

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-04-24 21:14:40 +08:00
Родитель 3d72d392d0
Коммит a57fe8d590
1 изменённых файлов: 9 добавлений и 4 удалений

Просмотреть файл

@ -85,6 +85,10 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q
}
req, err := cli.newRequest(method, path, query, body, headers)
if err != nil {
return serverResp, err
}
if cli.proto == "unix" || cli.proto == "npipe" {
// For local communications, it doesn't matter what the host is. We just
// need a valid and meaningful host name. (See #189)
@ -98,10 +102,6 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q
}
resp, err := cancellable.Do(ctx, cli.transport, req)
if resp != nil {
serverResp.statusCode = resp.StatusCode
}
if err != nil {
if isTimeout(err) || strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
return serverResp, ErrConnectionFailed
@ -110,6 +110,7 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q
if !cli.transport.Secure() && strings.Contains(err.Error(), "malformed HTTP response") {
return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?", err)
}
if cli.transport.Secure() && strings.Contains(err.Error(), "remote error: bad certificate") {
return serverResp, fmt.Errorf("The server probably has client authentication (--tlsverify) enabled. Please check your TLS client certification settings: %v", err)
}
@ -117,6 +118,10 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q
return serverResp, fmt.Errorf("An error occurred trying to connect: %v", err)
}
if resp != nil {
serverResp.statusCode = resp.StatusCode
}
if serverResp.statusCode < 200 || serverResp.statusCode >= 400 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {